I have these two codes I've done to load data on scroll and display it in two different divs (.cat_a and .cat_b).
Data is stored as objects in a json file and printed out 3 at a time from get_data.php.
I'm not familiar with JS and jQuery, is there a way to simplify and clean them?
Load data on (div class=".cat_a"):
<script type="text/javascript">
$(document).ready(function() {
var flag = 0;
$.ajax({
type: 'GET',
url: 'get_data.php',
data: {
'offset': 0,
'limit': 3,
'cat': "cat_a"
},
success: function(data) {
$('.cat_a').append(data);
flag += 3;
}
});
$(window).scroll(function() {
if ($(window).scrollTop() >= $(document).height() - $(window).height()) {
$.ajax({
type: 'GET',
url: 'get_data.php',
data: {
'offset': flag,
'limit': 3,
'cat': "cat_a"
},
success: function(data) {
$('.cat_a').append(data);
flag += 3;
}
});
}
});
});
</script>
Load data on (div class=".cat_b"):
<script type="text/javascript">
$(document).ready(function() {
var flag = 0;
$.ajax({
type: 'GET',
url: 'get_data.php',
data: {
'offset': 0,
'limit': 3,
'cat': "cat_b"
},
success: function(data) {
$('.cat_b').append(data);
flag += 3;
}
});
$(window).scroll(function() {
if ($(window).scrollTop() >= $(document).height() - $(window).height()) {
$.ajax({
type: 'GET',
url: 'get_data.php',
data: {
'offset': flag,
'limit': 3,
'cat': "cat_b"
},
success: function(data) {
$('.cat_b').append(data);
flag += 3;
}
});
}
});
});
</script>