Javascript Ajax无限滚动特性触发两次。
出于某种奇怪的原因,下面的ajax函数触发了两次。实验结果;函数用于从服务器检索数据。我使用googledeveloper工具跟踪发送到服务器的数据,结果显示ajax请求连续两次被触发。我猜当用户快速滚动时会发生这种情况Javascript Ajax无限滚动特性触发两次。,javascript,ajax,jquery,Javascript,Ajax,Jquery,出于某种奇怪的原因,下面的ajax函数触发了两次。实验结果;函数用于从服务器检索数据。我使用googledeveloper工具跟踪发送到服务器的数据,结果显示ajax请求连续两次被触发。我猜当用户快速滚动时会发生这种情况 //Executed when user has scrolled bottom of the page $(window).scroll(function (e) { if ($(window).scrollTop() == $(document).height() -
//Executed when user has scrolled bottom of the page
$(window).scroll(function (e) {
if ($(window).scrollTop() == $(document).height() - $(window).height()) {
loadMoreResults();
}
});
你知道如何防止这种情况发生吗?
感谢您的帮助。正如您所说,这可能是因为用户滚动太快,文档无法使用新结果进行更新 尝试使用一个标志,该标志将阻止在函数仍在执行时调用loadMoreResults 当函数启动时,将其设置为true,在得到结果后,将其设置为false。在将标志设置为true之前,可以将标志检查放在loadMoreresults函数的开头 例如:
正如您所说的,这可能是因为用户滚动太快,文档无法使用新结果进行更新 尝试使用一个标志,该标志将阻止在函数仍在执行时调用loadMoreResults 当函数启动时,将其设置为true,在得到结果后,将其设置为false。在将标志设置为true之前,可以将标志检查放在loadMoreresults函数的开头 例如:
不知什么原因,我搞不懂。设置国旗对我不起作用。函数调用仍将触发两次。 我必须在成功回调中使用超时
.then(function(resp) {
var i, len, ref, t;
ref = resp.data.users;
for (i = 0, len = ref.length; i < len; i++) {
t = ref[i];
$scope.users.push(t);
}
return setTimeout((function() {
if (resp.data.users.length > 0) {
return $scope.busy = false;
}
}), 500);
不知什么原因,我搞不懂。设置国旗对我不起作用。函数调用仍将触发两次。 我必须在成功回调中使用超时
.then(function(resp) {
var i, len, ref, t;
ref = resp.data.users;
for (i = 0, len = ref.length; i < len; i++) {
t = ref[i];
$scope.users.push(t);
}
return setTimeout((function() {
if (resp.data.users.length > 0) {
return $scope.busy = false;
}
}), 500);