我想阻止jQuery .scroll函数在执行.scroll函数内部运行时运行。 原因是因为它可能会运行两次并对我造成问题。防止jQuery .scroll函数在执行.scroll函数时运行
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() > $(document).height() - 50) {
// do stuff
}
我想:
var carRun = true;
if (canRun==true){
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() > $(document).height() - 50) {
canRun=false;
// do stuff
canRun=true;
}
}
,并没有解决问题。
编辑:
这些事情我试过,而不是为我工作测试: 一:
var canRun = true;
$(window).scroll(function() {
if (canRun == true){
if($(window).scrollTop() + $(window).height() > $(document).height() - 50) {
canRun = false;
// Are there more posts to load?
if (pageNum <= max) {
console.log(max);
// Show that we're working.
$('#loadmoreLink').text('Loading posts...');
$('.pbd-alp-placeholder-'+ pageNum).load(nextLink + ' .post',
function() {
// Update page number and nextLink.
console.log("The number of the next page to load: " + pageNum);
pageNum++;
console.log("The number of the next page to load after increment: " + pageNum);
nextLink = 'http://www.example.com.br/page/' + pageNum + '/';
console.log("The link of the next page: " + nextLink);
// Add a new placeholder, for when user clicks again.
$('#pbd-alp-load-posts')
.before('<div class="pbd-alp-placeholder-'+ pageNum +'"></div>');
console.log("--------END AJAX-------------");
// Update the button message.
if(pageNum <= max) {
$('#pbd-alp-load-posts a').text('Load More Posts');
} else {
$('#pbd-alp-load-posts a').text('No more posts to load.');
}
}
);
}
canRun = true;
}
}
});
二:
var canRun = true;
$(window).scroll(function() {
if(($(window).scrollTop() + $(window).height() > $(document).height() - 50) && (canRun == true)) {
canRun = false;
// Are there more posts to load?
if (pageNum <= max) {
console.log(max);
// Show that we're working.
$('#loadmoreLink').text('Loading posts...');
$('.pbd-alp-placeholder-'+ pageNum).load(nextLink + ' .post',
function() {
// Update page number and nextLink.
console.log("The number of the next page to load: " + pageNum);
pageNum++;
console.log("The number of the next page to load after increment: " + pageNum);
nextLink = 'http://www.example.com.br/page/' + pageNum + '/';
console.log("The link of the next page: " + nextLink);
// Add a new placeholder, for when user clicks again.
$('#pbd-alp-load-posts')
.before('<div class="pbd-alp-placeholder-'+ pageNum +'"></div>');
console.log("--------END AJAX-------------");
// Update the button message.
if(pageNum <= max) {
$('#pbd-alp-load-posts a').text('Load More Posts');
} else {
$('#pbd-alp-load-posts a').text('No more posts to load.');
}
}
);
}
canRun = true;
}
});
这一个曾工作:
jQuery(document).ready(function($) {
// The number of the next page to load (/page/x/).
var pageNum = parseInt(pbd_alp.startPage);
// The maximum number of pages the current query can return.
var max = parseInt(pbd_alp.maxPages);
// The link of the next page of posts.
var nextLink = pbd_alp.nextLink;
//what page am I on?
var whatPage = pbd_alp.theTitle;
//console.log(whatPage);
/**
* Replace the traditional navigation with our own,
* but only if there is at least one page of new posts to load.
*/
if(pageNum <= max) {
// Insert the "More Posts" link.
$('#content').append('<div class="pbd-alp-placeholder-'+ pageNum +'" ></div>')
.append('<p id="pbd-alp-load-posts"><a href="javaScript:void(0);" id="loadmoreLink" onclick="_gaq.push([\'_trackEvent\', \'Click\', \'LoadMore\', \''+ whatPage +'\']);">Load More Posts</a></p>');
// Remove the traditional navigation.
$('.navigation').remove();
}
/**
* Load new posts when the link is clicked.
*/
var timerId = false;
var canRun = true;
$(window).bind('scroll', function() {
if(canRun == true){
if($(window).scrollTop() + $(window).height() > $(document).height() - 50) {
canRun = false;
// Are there more posts to load?
if (pageNum <= max) {
console.log(max);
// Show that we're working.
$('#loadmoreLink').text('Loading posts...');
console.log("start loading things from ");
$('.pbd-alp-placeholder-'+ pageNum).load(nextLink + ' .post', function() {
console.log("Im in da callback, loading has finished");
// Update page number and nextLink.
//the target of load is '.pbd-alp-placeholder-'+ pageNum
//pageNum must'nt be changed before loading.
console.log("The number of the next page to load: " + pageNum);
pageNum++;
console.log("The number of the next page to load after increment: " + pageNum);
nextLink = 'http://www.example.com.br/page/' + pageNum + '/';
console.log("The link of the next page: " + nextLink);
// Add a new placeholder, for when user clicks again.
$('#pbd-alp-load-posts').before('<div class="pbd-alp-placeholder-'+ pageNum +'"></div>');
// Update the button message.
if(pageNum <= max) {
$('#pbd-alp-load-posts a').text('Load More Posts');
} else {
$('#pbd-alp-load-posts a').text('No more posts to load.');
}
//and now that I have finished, we can load more stuff
canRun = true;
});
}
}
}
});
});
你的if语句检查'canRun'是否真的需要在'scroll'处理函数中...... –
尝试过其他的东西: 'var canRun = true; ()($(window).scrollTop()+ $(window).height()> $(document).height() - 50)&&(canRun ==)$(window).scroll(function(){ if真)){ \t \t \t canRun = FALSE; //做的东西 canRun = TRUE;} }); ' 我做错了,因为它仍然无法正常工作。 –
评论对于代码中的小部分(单行)部分来说并不是很好,我建议您使用您尝试的任何其他代码编辑您的问题,并准确解释您遇到的问题。您可能还需要展开'// do stuff'来包含实际的代码 - 如果它调用了异步函数,那么'canRun = true'可能在您的代码实际完成之前运行。 –