Well, the problem is not that the scroller is at the bottom of the page, but with the way the event handler works. Sometimes the scroller will be at the bottom of the page, for example, say when there are no more messages to download ... for example, watch Facebook.
Currently, the jQuery scroll event is fired when a scroll occurs.
JQuery docs:
A scroll event is dispatched whenever a scroll position of an element changes, regardless of the reason. Clicking or dragging the scroll bar, dragging inside an element, pressing the arrow keys, or using the mouse scroll wheel can trigger this event.
Now your task for your script is to make one AJAX call to check if there is content to download. You need to modify your script to stop several AJAX calls during this time, and as I can see, @EJ Brennan has already suggested the same :).
You can add flags as follows:
//listen to scroll function $('#scrollbox').scroll(function(){ //[Kent] Before we service the event, we check if the last scroll event was handled/completed. //If it is not yet compelted, don't start another one and jump out of the code. if ($(window).data('ajax_in_progress') === true) return; //define what we need var scrolltop=$('#scrollbox').attr('scrollTop'); var scrollheight=$('#scrollbox').attr('scrollHeight'); var windowheight=$('#scrollbox').attr('clientHeight'); var scrolloffset=20; //get number of div which will append to script in case of limit database to page 2 or 3 or... size = $('#content > div').children().size(); //if we reach to bottom of div we are going to call to ajax function if(scrolltop>=(scrollheight-(windowheight+scrolloffset))){ $(window).data('ajax_in_progress', true); //[Kent] prevent more scroll events as AJAX request will soon begin. var form_data = $('#formdata').val(); // if remain of size(count of div) is 0 then we have more data to show because // we limit data provided by script to 7 field(we handle situation that we had // 14 or 21 respond from database in "next step" because if there is no data to // show we dont have to let script to run) if(size % 7 == 0){ //call to load data function setTimeout(function(){loaddata(form_data, size)}, 1500); }else{ //do nothing its just in case we need to append something like no more data to load } } }); //function to load data function loaddata(form_data, size){ number = "&size=" + size; //fetch new items $.post('dosearch.php', form_data+number, function(newitems){ // [Kent] This is the callback funciton that gets executed ONLY // when the AJAX request has completed. We will append the data // into the DOM and then reset the FLAG. //next step : if page echoing "" then do nothing if(newitems == ''){ }else{ //if we have data append these data to our div
source share