tags:

views:

785

answers:

2

My current web app project makes heavy use of ajax calls. Most of them are fast and respond almost immediately. So, showing ajax loader all the time is not necessary. But I want to show an ajax loader when ajax calls take longer then 250ms (or so). Otherwise the users might be confused and keep clicking on links over and over again. :)

Any ideas how to accomplish that using jQuery?

+3  A: 

I have no previous experience with jQuery but to achieve a simple delay to execution of some code, just use this:

setTimeout ("foo()", 250);

where foo() is the function responsible for loading... indication.

Peter Perháč
Also note that it's somewhat faster to use foo as a variable rather than a string, since if you use a string, it'll get eval'ed:setTimeout(foo, 250);
moff
@moff I like that suggestion, never tried yet, but will do from now on
Peter Perháč
+2  A: 

Can you use a Javascript timer to trigger after 250ms to display your 'loading...' message ?

Activate this on ajaxSend(), and disable it on ajaxComplete() (and clear the 'loading...' message if reqd), and so the implementation should be transparent to whatever Ajax calls you make.

Brian Agnew