我想阻止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'可能在您的代碼實際完成之前運行。 –