I would set the boolean to true before making my request, and whenever the request completes, I would return it to false. Then I would wrap the code that makes the request in checking if this value is true or false. I would also add a bool that tells me whether I should even worry about making a request - it makes no sense to ask if the last request is returned empty (if perhaps the data set has not changed since the last request). Anyway, here is the code that I would start with:
( function( global ) { var $ = global.jQuery, $win = $( global ), $doc = $( global.document ), $ajaxLoader = $( 'div#ajaxloader' ), $main = $( '#main' ), requestInProgress = false, outOfPosts = false; $win.scroll( function() { if( ! requestInProgress && ! outOfPosts && $win.scrollTop() === $doc.height() - $win.height() ) { requestInProgress = true; $ajaxLoader.show(); $.ajax( { url: 'loader.php', data: { lastid: $( '.container:last' ).attr( 'id' ) }, success: function( html ) { if( html ) { $main.append( html ); $ajaxLoader.hide(); } else { outOfPosts = true; $ajaxLoader.html( 'No more posts to show.' ); } }, complete: function() { requestInProgress = false; } } ); } } ); }( window ) );
source share