How to display a "busy" indicator with jQuery?
You can just show / hide a gif, but you can also embed that to ajaxSetup, so it's called on every ajax request.
$.ajaxSetup({
beforeSend:function(){
// show gif here, eg:
$("#loading").show();
},
complete:function(){
// hide gif here, eg:
$("#loading").hide();
}
});
One note is that if you want to do an specific ajax request without having the loading spinner, you can do it like this:
$.ajax({
global: false,
// stuff
});
That way the previous $.ajaxSetup we did will not affect the request with global: false
.
More details available at: http://api.jquery.com/jQuery.ajaxSetup
The jQuery documentation recommends doing something like the following:
$( document ).ajaxStart(function() {
$( "#loading" ).show();
}).ajaxStop(function() {
$( "#loading" ).hide();
});
Where #loading
is the element with your busy indicator in it.
References:
- http://api.jquery.com/ajaxStart/
http://api.jquery.com/ajaxStop/
-
In addition,
jQuery.ajaxSetup
API explicitly recommends avoidingjQuery.ajaxSetup
for these:Note: Global callback functions should be set with their respective global Ajax event handler methods—
.ajaxStart()
,.ajaxStop()
,.ajaxComplete()
,.ajaxError()
,.ajaxSuccess()
,.ajaxSend()
—rather than within theoptions
object for$.ajaxSetup()
.
I tend to just show/hide a IMG as other have stated. I found a good website which generates "loading gifs"
Link
I just put it inside a div
and hide by default display: none;
(css) then when you call the function show the image, once its complete hide it again.