Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Infinite Scroll - event fires multiple times when scrolling is fast

Tags:

Okay, the code below "works" in that when you scroll to the bottom of the page the AJAX function is fired and results are appended to the #postswrapper div on the page.

The issue is: if I scroll really quickly when I reach the bottom of the page, the AJAX fires several times and loads several sets of results into the #postswrapper div (number of additional, 'unwanted' results are determined by how many additional scroll events were fired by scrolling quickly).

Ultimately, I need only serve one set of results per time the user reaches the bottom. I've tried adding timers and such, but to no avail. It's obviously storing the additional scroll actions in the DOM and firing them in sequential order thereafter.

Any ideas?

I'm using jquery.1.4.4.js if that helps anybody. And e.preventDefault() doesn't work, in this situation, anyways.

$(window).scroll(function(e) {     e.preventDefault();     if ($(window).scrollTop() >= ($(document).height() - $(window).height())) {         $('div#loadmoreajaxloader').show();         $.ajax({             cache: false,             url: 'loadmore.php?lastid=' + $('.postitem:last').attr('id'),             success: function(html) {                 if (html) {                     $('#postswrapper').append(html);                     $('div#loadmoreajaxloader').hide();                 } else {                     $('div#loadmoreajaxloader').html();                 }             }         });     } }); 
like image 389
Marc Avatar asked Aug 03 '11 20:08

Marc


People also ask

Does infinite scroll improve performance?

Above all else, infinite scroll is designed to boost user engagement and keep viewers on the page for as long as possible. If visitors have no particular goal in mind, infinite scrolling will continue to roll out relevant content in a way that is efficient, digestible, and interruption-free.

How do I stop infinite scrolling?

Use a browser plugin to control infinite scrolling within your web browser. To avoid the extra memory usage, you can block all JavaScript entirely on your computer in your system settings. Be aware that disabling JavaScript may also disable the functionality of other websites.

Should you use infinite scroll?

Infinite scrolling is best for a “browsing” mindset They do this for two key reasons: it boosts user engagement and is better for mobile devices. Users don't have to click buttons to load up the next batch of results: they can scroll, which is an easy action for smartphones.

What is endless scrolling called?

What Is Infinite Scroll? A web design technique where, as the user scrolls down a page, more content automatically and continuously loads at the bottom, eliminating the user's need to click to the next page. The idea behind infinite scroll is that it allows people to enjoy a frictionless browsing experience.


1 Answers

Try storing some kind of data that stores whether the page is currently loading new items. Maybe like this:

$(window).data('ajaxready', true).scroll(function(e) {     if ($(window).data('ajaxready') == false) return;      if ($(window).scrollTop() >= ($(document).height() - $(window).height())) {         $('div#loadmoreajaxloader').show();         $(window).data('ajaxready', false);         $.ajax({             cache: false,             url: 'loadmore.php?lastid=' + $('.postitem:last').attr('id'),             success: function(html) {                 if (html) {                     $('#postswrapper').append(html);                     $('div#loadmoreajaxloader').hide();                 } else {                     $('div#loadmoreajaxloader').html();                 }                 $(window).data('ajaxready', true);             }         });     } }); 

Right before the Ajax request is sent, a flag is cleared signifying that the document is not ready for more Ajax requests. Once the Ajax completes successfully, it sets the flag back to true, and more requests can be triggered.

like image 115
Matt Bradley Avatar answered Sep 28 '22 07:09

Matt Bradley