[英]How to run Ajax call on scroll every time when previous ajax call completes
我在滚动时调用 ajax 函数,我遇到的问题是,当用户到达调用 ajax 函数的点向下滚动时,ajax 调用被触发,并且在使用 ajax 调用加载数据/记录时,滚动是运行 ajax 调用 100 次。 我需要的是当用户滚动到特定点时,ajax 函数只调用一次,直到从 ajax 检索记录。 检索记录后,ajax 调用应再次触发,依此类推。
jQuery.noConflict($);
jQuery(document).ready(function($) {
$(window).scroll(function() {
var that = $('#loadMore');
var page = $('#loadMore').data('page');
var newPage = page + 1;
var ajaxurl = $('#loadMore').data('url');
//
if ($(window).scrollTop() >= $(document).height() - $(window).height() - 100 && $tester == true) {
console.log("ajax triggered mobile");
//ajax call
$.ajax({
url: ajaxurl,
type: 'post',
data: {
page: page,
action: 'ajax_script_load_more'
},
error: function(response) {
console.log(response);
},
success: function(response) {
//check
if (response == 0) {
//check
if ($("#no-more").length == 0) {
$('#ajax-content').append('<div id="no-more" class="text-center"><h3>You reached the end of the line!</h3><p>No more posts to load.</p></div>');
}
$('#loadMore').hide();
} else {
$('#loadMore').data('page', newPage);
$('#ajax-content').append(response);
}
}
});
}
很久以前在某个地方发现了这颗宝石。 应该为你工作,让我知道。
var scrolled = false;
page.on('scroll', function(){
if(!scrolled){
scrolled = true;
//do stuff that should take a while...
scrolled = false;
};
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.