我在我的幾個頁面上設置了無限滾動功能。它工作得非常好,但Ajax調用來加載更多的項目會進行多次數據庫調用,並且每次都必須加載大量圖像,並且通常需要幾秒鐘才能加載。我根據我的連接在3到7秒之間計時。因此,當瀏覽器決定多次觸發滾動事件時,它會變成真正的火車殘骸。我怎麼能去限制或反彈它,以便瀏覽器在幾秒鐘內不會多次運行Ajax調用並將所有內容都暫停?Javascript無限滾動限制/反彈
$(document).ready()
{
//Infinite scroll
$(window).on('scroll', _.debounce(function(){
var height = $(window).height();
var scrollTop = $(window).scrollTop();
var dHeight = getDocHeight();
if(height + scrollTop >= dHeight - 100)
{
//Display items that were previously hidden
showAllItems();
if(!vloaded < START_NUM && !done){
//Load next set of items
$.ajax
({
type: "POST",
url: "/organizer/getMore",
data: { filter: vfilter, loaded: vloaded },
dataType: "html",
success: function(responseText, textStatus, XHR)
{
if(responseText.indexOf("// GRID ENTRY //") < 0){ //Out of items to load
done = true;
}
else{
//Display items that were previously hidden
showAllItems();
// select the element with the ID grid and insert the HTML
$("#grid").append(responseText);
//alert("Loaded "+vloaded+" items");
}
}
});
// global variable
vloaded +=LOAD_NUM;
} // if
}
}
}, 500, true)); // on
} // ready
編輯:
我下載了下劃線,並且增加了防抖動功能,但現在的Ajax調用似乎並沒有在所有運行。即使我沒有立即設置爲true,它也會在停止滾動後很快執行,但它根本不會執行。