Answer the question
In order to leave comments, you need to log in
Conflict of two ajax requests - infinite scroll and filter. How to reload the script?
I have a simple php pagination and a js script that, when scrolling the page to the very bottom, gets new posts from the next pagination page.
I'm also trying to add a script that, when clicking on the filters, loads posts from a given category also via ajax.
Filter script:
$(document).on('click', '.ajax-js', function(e){
e.preventDefault();
console.log('filter click');
var link = $(this).attr('href');
$.ajax({
url: link,
dataType: 'html',
action: $("html, body").animate({ scrollTop: 0 }, 200),
success: function(data) {
var replacement = $('<div />').append(data).find('#replacement-js').html();
$('#replacement-js').html(replacement);
history.pushState(null, null, link);
},
});
});
jQuery(document).ready(function($){
var next = 2;
var progress = false;
$(document).on('scroll', function() {
if($(window).scrollTop() + $(window).height() >= $(document).height() && !progress) {
console.log('pagination '+next);
$.ajax({
url: location.href,
dataType: 'html',
method: 'get',
data: "nav="+next,
beforeSend: function() {
progress = true;
},
success: function(data) {
var result = $('<div />').append(data).find('.posts-box').html();
$('.posts-box').append(result);
progress = false;
next +=1;
}
});
}
});
});
function myFunc() {
// stuff...
}
Answer the question
In order to leave comments, you need to log in
You don't need to store the pagination state in the next variable, but get it on the fly right before sending the request. I don't know how your page is set up. For example, from a global variable, or somehow determined by dom, or by the current link (if it changes), and so on. And then, when changing filters, reset this state.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question