views:

285

answers:

2

Hi,

what is the best way to show a spinner?

I have prepared a div(id="spinner"), that should be visible during loading.

Best regards

+2  A: 

Do you use jQuery?

If so you can use:

ajaxStart & ajaxStop: http://docs.jquery.com/Ajax

For example:

$(function(){

// hide it first
$("#spinner").hide();

// when an ajax request starts, show spinner
$.ajaxStart(function(){
    $("#spinner").show();
});

// when an ajax request complets, hide spinner    
$.ajaxStop(function(){
    $("#spinner").hide();
});

});

You can fine tune a little with a request counter that increments and decrements in case you have a lot of simultaneous requests.

If you don't use jQuery, check out the jQuery Source code for which events ajaxStart actually register in plain old javascript.

HTH Alex

AlexDuggleby
I am using prototype.
brainfck
Ok not quite as easy, but check this page http://www.prototypejs.org/api/ajax/request and the events:- onCreate- onComplete
AlexDuggleby
Thanks, I have changed to jQuery. Prototype sucks:)
brainfck
+1  A: 
$().ajaxSend(function(r, s) {
    $("#spinner").show();
});

$().ajaxStop(function(r, s) {
    $("#spinner").fadeOut("fast");
});
Tony Borf