Je veux empêcher la fonction jQuery .scroll de s'exécuter en même temps que la fonction .scroll. La raison est parce qu'il peut courir deux fois et causer des problèmes pour moi.Empêche la fonction .scroll de jQuery de s'exécuter en même temps que la fonction .scroll
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() > $(document).height() - 50) {
// do stuff
}
J'ai essayé:
var carRun = true;
if (canRun==true){
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() > $(document).height() - 50) {
canRun=false;
// do stuff
canRun=true;
}
}
et il n'a pas résolu le problème.
Edit:
ce sont des tests de choses que j'essayé et ne fonctionnent pas pour moi: n ° 1:
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;
}
}
});
n ° 2:
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;
}
});
Celui-ci travaillé:
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;
});
}
}
}
});
});
Votre instruction if pour vérifier si 'canRun' est true besoin sorte de être à l'intérieur de la fonction de gestionnaire' scroll' ... –
a essayé somwthing autre: 'var CANRUN = true; $ (window) .scroll (function() { if (($ (fenêtre) .scrollTop() + $ (fenêtre) .height()> $ (document) .height() - 50) && (canRun == true)) { \t \t \t CANRUN = false; // faire des choses CANRUN = true;} }); ' Est-ce que je fais mal parce que ça ne fonctionne toujours pas. –
Les commentaires ne sont pas vraiment parfaits pour plus de petites sections de code (une seule ligne), je suggère de modifier votre question avec d'autres morceaux de code que vous avez essayés, et d'expliquer exactement quel problème vous voyez. Vous devrez peut-être aussi développer '// do stuff' pour inclure le code réel - si cela appelle des fonctions asynchrones alors' canRun = true' peut être en cours d'exécution avant que votre code ne soit terminé. –