开发者

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

开发者 https://www.devze.com 2023-02-06 07:51 出处:网络
How can I set ajax success for all requests done? $.ajax({ success: function(){ c开发者_运维知识库licked = false;}

How can I set ajax success for all requests done?

$.ajax({
  success: function(){ c开发者_运维知识库licked = false;}
});

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


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.


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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号