[英]jquery .load function running more than once
我正在嘗試根據google的建議實現SEO友好的無限滾動,如此處所示( http://scrollsample.appspot.com/items?page=7 )。 我有一個jquery函數,可以在有人滾動到頁面底部時向php文件發送請求(該請求從db中獲取數據),現在一切正常,除了用戶滾動到頁面底部時,請求函數會被多次觸發。 因此,重復的數據條目被加載到頁面中,現在我知道這不是來自我的php文件,因為我直接在瀏覽器中打開了頁面,一切都很好。 在此處檢出錯誤http://devx.dx.am/haze/categor.php?artemis=foo&&page=1
我已經在這里嘗試了解決方案( jQuery .load()回調函數觸發了多次 ),在這里( $(window).load()執行了2次? ),還有其他一些解決方案。
$(window).bind('scroll', function() { //#cagorwrap is the div that should contain the data retrieved
if($(window).scrollTop() >= $('#cagorwrap').offset().top + $('#cagorwrap').outerHeight() - window.innerHeight) { //344.6
var queryParameters = {}, queryString = location.search.substring(1),
re = /([^&=]+)=([^&]*)/g, m;
while (m = re.exec(queryString)) {
queryParameters[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}
var url2 = "modules/paginate.php?numpages=set";
// #rc is a hidden div too
$("#rc").load(url2, function() {
var rc = $(this).html();
if (queryParameters['page'] < rc) {
queryParameters['page']++;
console.log(rc);
var stateObj = queryParameters['page'];
let cagh = $('#cagorwrap').height() + 344.6 - 75;
$("#cagorwrap").height(cagh);
history.pushState(null, null, "categor.php?artemis=cat&&page="+stateObj);
var url = "modules/paginate.php?artemis=cats&&page="+stateObj;
$("#obtainer").load(url, function () {
$("#cagorwrap").append($(this).html());
}); //#obtainer is a hidden div that receives the data at first before it is appended to #cagorwrap
} else{
//unbind scroll here
}
});
}
});
如果所有其他方法都失敗了,並且您絕對需要解決方案,那么可以添加一個counter=1;
在開始時,僅在以下情況下觸發請求功能
counter++;
if (counter%2==0){//fire request}
這不是干凈的,但是如果您為此花了太多時間,並希望稍后再解決問題...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.