Callback After Multiple Ajax And Non-ajax Functions
I'm learning to perform a callback after successful results from two functions, one of which is ajax and one is non-ajax (both are asynchronous). In my script there is a non-ajax c
Solution 1:
$.when will actually take in multiple deferred objects, so you can do something like this:
var xhr = ajax();
var images_promise = loadImages();
$.when.apply($, [xhr, images_promise]).done(function () {
// something to do when both are complete
});
Provided that the ajax
and loadImages
functions return promise objects:
functionajax() {
return $.ajax({
// ajax configuration
});
}
functionloadImages() {
// create the deferred promise objectvar dfd = new jQuery.Deferred();
$("img").on('load', function() {
// on the load event resolve the promise
dfd.resolve();
});
return dfd;
}
Read more about deferred promises here
Post a Comment for "Callback After Multiple Ajax And Non-ajax Functions"