Show ajax loading indicator not immediately, but after a delay

My current web application project is heavily using ajax calls. Most of them are quick and react almost immediately. Thus, showing the ajax bootloader all the time is not required. But I want to show the ajax loader when ajax calls take longer than 250 ms (or so). Otherwise, users may be confused and constantly click on links again and again. :)

Any ideas on how to do this using jQuery?

+5
source share
3 answers

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

setTimeout ("foo()", 250);

foo() - , ...

+4

Javascript 250 "..."?

ajaxSend() ajaxComplete() ( "...", reqd), Ajax, .

+2
var activity = false;
$("#loadingIndicator").ajaxStart(function() {
    activity = true;
    window.setTimeout(function() {
        if (activity) {
            //alert('activity');
            $("#loadingIndicator").show();
        }
    }
    , 700);
});
$("#loadingIndicator").ajaxStop(function() {
    activity = false;
    $(this).hide();
});
+1
source

All Articles