I need to run a function after a series of ajax has been resolved. The problem is that the series of ajax calls are in a $.each
loop. I have tried using $.when
around the $.each
loop and then running the .done()
function on it, but everything in the .done()
is still running before the ajax is finished in the $.each
loop. Here is my code -
$.when(
$.each(images, function(key, value){
var getFile = $(value).attr('src');
$.ajax({
type: 'HEAD',
url: getFile,
success: function() {
var index = images.index(key);
if (index > -1) {
images.splice(index, 1);
}
},
error: function() {
}
});
x++;
});
).done(function(){
// code to be executed after ajax done.
});
I had this working by using async: false,
but I learned that is now deprecated and provides a terrible user experience for various reasons. Does anyone have any ideas? This is stumping me...