misima
misima

Reputation: 423

How do I set the "success" function for multiple ajax requests?

How can I set ajax success for all requests done?

$.ajax({
  success: function(){ clicked = false;}
});

$('.button').click(function(){
   $.post('file.php',{},function(){});
   $.post('file.php',{},function(){});
   $.post('file.php',{},function(){});
});

Upvotes: 3

Views: 796

Answers (2)

karim79
karim79

Reputation: 342665

Do you mean execute a single method once all requests have completed?

var counter = 0;
var nRequests = 3;

function multiSuccess() {
    // do something    
}

$('.button').click(function(){
   $.post('file.php',{},function(){
       counter++;
       if(counter == nRequests) {
           multiSuccess();
       }
   });
   $.post(...
   $.post(...
});

Also, you might want to look into ajaxStop:

Whenever an Ajax request completes, jQuery checks whether there are any other outstanding Ajax requests. If none remain, jQuery triggers the ajaxStop event. Any and all handlers that have been registered with the .ajaxStop() method are executed at this time.

Upvotes: 2

sth
sth

Reputation: 229744

You can use ajaxSetup() to specify default parameters for ajax calls:

$.ajaxSetup({
  success: function(){ clicked = false; }
});

Upvotes: 0

Related Questions