I’m trying to clear a busy icon and re-enable my delete button once the multiple jquery posts have completed. Here is my current code:
$('#deleteimgs').live('click', function(e) {
e.preventDefault();
if ($('input[name="chk[]"]:checked').length > 0 ) {
$('#deleteimgs').button('loading');
$('#saveicon').show();
var boxes = $('input[name="chk[]"]:checked');
$(boxes).each(function(){
var id = $(this).attr('id').substr(7);
var self = this;
$.post("/functions/photo_functions.php", { f: 'del', imgid: id}, function(data){
if (data.success) {
$(self).hide();
$("#img_"+id).hide(250);
}
}, "json");
});
$('#saveicon').hide();
$('#deleteimgs').button('reset');
}
});
My hide call and reset call are being trigger prior to completion of the foreach loop. Is there a way to wait for completion before making these two calls?
$('#saveicon').hide();
$('#deleteimgs').button('reset');
You should use the
successcallback method to complete any tasks once the HTTP request has finished. Inside of the callback you should keep track of how many requests have completed, and when your last request has finished, then execute the code of your desire.You could restructure this code in a few ways, but, it should give you a general idea of how to handle the situation.