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; }
});
精彩评论