I am using javascript scroll pagination in my website. When a page is srcolled down, some items are loaded from database. The issue is that, sometime, scroll pagination is getting the same data from database. I have used async: false
. But, it slowed the loading down.
Javascript code:
if (check !== null) {
$(window).scroll(function(ev) {
if ($(window).scrollTop() + window.innerHeight >= $('#footer').offset().top) {
var last_id = $(".item:last").data("id");
loadData(last_id);
};
});
}
var dataCount = 1;
function loadData(last_id) {
$.ajax({
url: 'some/url/loader',
type: "get",
data: {
id: last_id
},
// async: false,
success: function(data) {
if (data) {
dataCount++;
$('#div').append(data);
check = 1;
} else if (!data) {
console.warn('no data');
check = null;
}
}
});
}
Loader method in a controller:
public function actionLoader() {
$id = $_GET['id'];
$cat_id = $_GET['cat_id'];
$posts = SomeModel::getItems($id);
if (!empty($posts)) {
$q = $this - > renderPartial('view', array('data' => $posts));
echo $q;
}
}