I have some jQuery code to check if I've scrolled to the bottom of the window.
$(window).scroll(function(){
if($(window).scrollTop() + $(window).height() == $(document).height()) {
appendToGrid();
}
})
My appendToGrid() function scrolls the user to the top of the page and adds content. Problem is, I need this function called once per scroll. As I have it now, it is called multiple times per scroll.
If I change it to
$(window).one('scroll',function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
appendToGrid();
}
});
it will only fire once total, but I need it to fire once per scroll so the user can scroll to the bottom and keep getting sent back to the top of the page.
I've also tried the below but it still fires multiple times.
var fired = false;
$(window).scroll(function(){
if($(window).scrollTop() + $(window).height() == $(document).height() && !fired) {
fired = true;
appendToGrid();
fired = false;
}
})
You could add a cooldown timer once appendToGrid is called. This is similar to your
fired
flag but it only resets after a 2000ms wait. You can adjust that time to whatever feels best.Another option would be to throttle the logic so it only happens after the user stops the action for a period of time.