[英]Ajax function firing multiple times when user scrolls to bottom
這很可能是重復的,是的,我在這里和這里都看到過它們。 我的問題與第二個問題非常相似。
第二個鏈接建議要解決此問題,必須執行以下操作:
function loadMoreResults() {
if (flag) return;
flag = true;
[...]
flag = false;
}
我已經嘗試在我的實際代碼中安裝這樣的邏輯,但是無論()和{}是否已正確打開和關閉,它都會給出語法錯誤。 也許我做錯了。
$(document).ready(
function loadAll (param) {
if (param < 0) {
return;
} else if (isNaN(param)) {
param = param.responseText;
} else if (!isNaN(param)) {
}
$.ajax({
url: '../login/functionLogin/functions.php',
type: 'POST',
dataType: 'JSON',
data: {task: "load", next_id: param},
success: function(data) {
next_id = data.next_id;
$.each(results, function(i, attr) {
//Do whatever and return next_id
}
})
}
});
//Since the next_id is within the function, I do not have to make it global.
$(window).scroll(function () { //If user scrolls to bottom, fire anything inside
if ($(window).scrollTop() >= $(document).height() - $(window).height()) {
loadAll(next_id).die(); //Firing twice or even thrice
}
})
});
問題: 如何停止當前腳本兩次觸發? 這個腳本的結構好嗎? [我強烈認為這是非常糟糕的設計,並且會導致此問題]
我可以詳細說明。 我盡了最大的努力講這個問題。
看來您回答了自己的問題! 添加變量並檢查是否未定義,然后進行處理;如果已經設置,則不執行任何操作。
$(window).scroll(function () { //If user scrolls to bottom, fire anything inside
if ($(window).scrollTop() >= $(document).height() - $(window).height()) {
if (typeof flag != 'undefined' && flag) return;
loadAll(next_id); // .die(); // what is the die(); ??? //Firing twice or even thrice
flag = true;
}
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.