tags:

views:

54

answers:

3

Hi,

Why do I not see the spinner image when I run this code? However when I debug through it using firebug I do see the text/spinner image at the beginning:

<div id="spinner">
  <img src="/images/ajax-loader.gif"/>
  Text
</div>

<div id="events">
</div>

<script type="text/javascript" charset="utf-8">

 function load_events() {
   $("#events").load("robots.txt");
 }

 $(document).ready(function(){
   $("#spinner").show()
   $("#events").hide()

   setTimeout("load_events()",2000);
   $("#events").show()
   $("#spinner").hide()

 });

</script>

thanks

PS. In particular I need it so the spinner will keep showing until the AJAX response from the "load" actually all comes back (not until the load event itself starts, as the API I'm calling then still takes a couple of seconds before it comes back with the content)

PSS. Latest code I'm using after feedback. Still suffers from issue noted in the PS above.

<div id="spinner"> <img src="/images/ajax-loader.gif"/> </div>

<div id="events"> </div>

<script type="text/javascript" charset="utf-8">

 $(document).ready(function(){
   $("#spinner").show();
   $("#events").hide();

   setTimeout(function(){
     $("#events").load("weekends/concerts_display");
     $("#events").show();
     $("#spinner").hide();
   },10);

 });

</script>
A: 
setTimeout(load_events,2000);

Note that here you send a function, not a string.

You use also a unnamed function:

setTimeout(function(){
    $("#events").load("robots.txt");
},2000);
Mendy
+3  A: 

The lines of code after the timeout run immediately, consider using something like this:

$(document).ready(function(){
    $("#spinner").show();
    $("#events").hide();

    setTimeout(function(){
        $("#events").load("robots.txt", null, function(){
            $("#events").show();
            $("#spinner").hide();
        });
    },2000);
});
Jonathan
thats working better - but the spinner itself still disappears a second or two before the HTML contents of the load comes across (as in fact the actual URL I am using is not robots.txt, but rather a pointer to one of my data APIs). Is there a way to get this code so that the spinner will keep showing until the AJAX response from the "load" actually all comes back?
Greg
Sure is, I've modified the code above to use the callback feature. The callback is called when the data has been loaded. See http://docs.jquery.com/Ajax/load#urldatacallback
Jonathan
thanks - just saw your comment here after I saw the ajaxStart/ajaxStop suggestion (which seemed to do the trick)...
Greg
+1  A: 

You might also consider using jQuery's ajaxStart and ajaxStop methods for showing and hiding your gif, rather than a setTimeout. The nice thing about doing it this way is that it should kick in for any ajax on the page, so you can program it once and forget it.

$('body').ajaxStart(function(){
  $('#spinner').show();
});
$('body').ajaxStop(function(){
  $('#spinner').hide();
});
Greg W
this worked and did the trick - satisfied my requirement from my PS above
Greg