【发布时间】:2010-11-09 17:26:47
【问题描述】:
我有这个代码:
$(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);
});
}
});
});
这会检查用户是否靠近页面底部并加载更多内容。问题是如果用户在临界点附近缓慢滚动,或者一次又一次地快速上下滚动,$.post 函数会运行几次,这意味着您最终会得到一些我正在加载的数据实例.
我曾尝试绑定和解除绑定e 变量,但效果不佳。是否有可能运行一次post 函数,然后重置该函数,以便当用户再次向下滚动时它会再次运行,因此不会加载多个数据实例?
【问题讨论】:
标签: javascript jquery pagination