我有這樣的代碼:重置一個函數,或者停止某些操作並重新啓動它?
$(document).ready(function() {
var number = 10;
var offset = 10;
var page_number = 2;
/* Bind the scroll function to an event */
$(window).bind('scroll', function(e) {
/* If the scroll height plus the window height is
more than the document height minus 10, continue */
if($(window).scrollTop() + $(window).height() >
$(document).height() - 10) {
/* Quick message so you know more stuff is loading */
$('.loading-more').html('Keep scrolling for more posts to load..');
$.post('<?php bloginfo('siteurl') ?>/wp-admin/admin-ajax.php', {
action: 'and_action',
off: offset+number,
pagenumber: page_number - 1
}, function(data) {
offset = offset+number;
$('.empty-div').append('<p><strong>Page '+page_number+'</strong></p><br />'+data);
page_number += 1;
$(this).unbind(e);
});
}
});
});
此檢查用戶是否是靠近頁面和負載更多的內容的底部。問題在於,如果用戶在臨界點附近緩慢滾動,或者反覆滾動一遍又一遍,速度很快,則函數會運行幾次,這意味着您最終會加載一些我正在加載的數據實例。
我曾嘗試過的做法是綁定和解除綁定e
變量,但它並沒有很好的工作。無論如何可能會運行一次函數post
,然後重新設置函數,所以當用戶再次滾動時它會再次運行,所以不會加載一個以上的數據實例?