views:

78

answers:

3

Is there a way to have the callback of one AJAX trigger another AJAX call? Or will that go out of scope?

+1  A: 

You should be able to do this. Closures keep everything in scope.

Matthew Flaschen
+1  A: 

In addition to Matthew's answer, you can also use setTimeout() to repeat the AJAX call at a predefined interval, as in the following example:

function autoUpdate() {

   $.ajax({
      type: 'GET',
      url:  '/web-service/?no_cache=' + (new Date()).getTime(),

      success: function(msg) {
         // Add your logic here for a successful AJAX response.
         // ...
         // ...

         // Relaunch the autoUpdate() function in 5 seconds
         setTimeout(autoUpdate, 5000);
      }
   });
}
Daniel Vassallo
+2  A: 

Sure...

...
success: function(data){
     $.ajax({.....
}
....

and as stated before, your scope is captured by the closure.

Sky Sanders