This is most likely a duplicate and yes, I have seen them here and here . My question is fairly similar to the second one.
The second link suggests that to fix this problem, one must do something like this:
function loadMoreResults() {
if (flag) return;
flag = true;
[...]
flag = false;
}
I have tried installing such logic within my actual code but all it does is give syntax errors no matter if the ()'s and {}'s have been opened and closed correctly. Perhaps I have done it wrong.
$(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
}
})
});
The question: How can I stop my current script from firing twice? Is the structure of this script good? [I strongly feel like it is very bad designed and the one causing this problem]
I can elaborate more. I tried my best wording this question.
It seems that you answered your own question ! add a variable and check if it is not defined then do the processing, if already set then don't do anything.
$(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;
}
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.