変数を使用して、未処理のajaxリクエストが存在するかどうかをマークするだけです。その場合は、別のリクエストを行わないでください。それ以外の場合は、データをフェッチします。私の考えでは、ユーザーにスクロールさせて、UIをブロックする理由を説明します。その場合、それは同期呼び出しを行うようなものです。
var isLoading = false;
//event handler
$(window).scroll(function () {
// check height..
// make an ajax request if isLoading is false
if (!isLoading) {
$.ajax(/* .. */, success: function () {
isLoading = false;
},
error: function () {
isLoading = false;
});
isLoading = true;
}
}
それでもajax応答までイベントのリッスンを停止したい場合は、バインディングを削除して、ajax応答ハンドラーに再度アタッチします。
var isLoading = false;
addScrollEvent();
function scrollHandler(e) {
// check height..
// make an ajax request if isLoading is false
if (!isLoading) {
$.ajax(/* .. */, success: function () {
isLoading = false;
addScrollEvent();
},
error: function () {
isLoading = false;
addScrollEvent();
});
$(window).off('scroll', scrollHandler);
}
}
function addScrollEvent() {
$(window).on('scroll', scrollHandler);
}