views:

20

answers:

2

I have a page when, if the user is near the bottom of the page, loads the next page.

I just recently switched it to jQuery, and now use the jQuery.ajax() function to get the data. However, now i cannot check if the xhr is already loading, making the page load multiple xhrs when the user near the bottom.

My listener is:

$(document).scroll(function () {
            if(/* scrollbar is near bottom */)
            loadxhr(dat++); //function that calls jQuery.ajax()
        }
});

Basically, can you track the readyState of the call in a global scope?

+1  A: 

I would just have a variable (outside the function)

var scrolling = 0;

When the time comes to call the ajax function, check if scrolling equals 0 and otherwise do nothing; if it IS equal, set it to 1, call the ajax function, and set it back to 0 in the latter's callback.

Kind of like the equivalent of a "lock" in preemptive multithreading systems, except that you don't really need a lock here (good thing too since javascript doesn't have them;-) because your thread of control is only interrupted when you call the ajax function, not at arbitrary spots in your code like in preemptive multithreading.

Alex Martelli
+1  A: 

maybe check out .ajaxStart() and .ajaxStop() for events that will inform you when calls are underway?

Scott Evernden