> > > From: Michael Geary
> > >
> > > Also, it looks like you have a race condition. What 
> > > happens if the entire AJAX request completes in
> > > less than 100 milliseconds? 
> > >
> > > http://www.pastebin.ca/678318

> > From: barophobia
> >     
> > I assumed that $.ajaxStart() would be canceled and 
> > $.ajaxStop() would execute.
> >     
> > I looked over your code but I don't see how the flag 
> > (called active) changes anything. Would you explain?
> > 
> > http://www.pastebin.ca/678388

> From: Erik Beeson
> 
> If ajaxStop is called before the setTimeout callback in 
> ajaxStart is run, active will be false, so the addClass() on 
> line 9 won't get executed. Without this, if ajaxStop ran 
> before the setTimeout callback, the removeClass() would 
> happen before the addClass(), so the class would never
> get removed. 

Thanks for explaining it, Eric.

I can't resist a little code cleanup, so here is a better version:

http://www.pastebin.ca/679254

In this one, instead of setting a flag, the code clears the timer, a more
direct approach to things.

(In case you're wondering after reading the code, yes, it may call
clearTimeout(timer) when timer is null, and no, that's not an error.)

One other little improvement: The code sets $loading = $('#loading') at the
beginning and uses that instead of the "var element = this" business. And
finally, just for fun, I made the whole thing an execute-in-place anonymous
function.

-Mike

Reply via email to