Created
March 17, 2015 19:59
-
-
Save msrafi/9355f224b89ef3da7746 to your computer and use it in GitHub Desktop.
jQuery: Ajax in loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Extending jQuery.when | |
if (jQuery.when.all === undefined) { | |
jQuery.when.all = function(deferreds) { | |
var deferred = new jQuery.Deferred(); | |
$.when.apply(jQuery, deferreds).then( | |
function() { | |
deferred.resolve(Array.prototype.slice.call(arguments)); | |
}, | |
function() { | |
deferred.fail(Array.prototype.slice.call(arguments)); | |
} | |
); | |
return deferred; | |
} | |
}; | |
var ajax_caller = function(obj) { | |
return $.ajax({ | |
method: obj.mthod, | |
url: obj.url, | |
async: true, | |
dataType: obj.type, | |
data: obj.data, | |
success: function(data){ | |
console.log(typeof data); | |
} | |
}); | |
}; | |
var ajaxData = []; | |
$.each(TIAA_ud.ajaxCalls, function(){ var obj = arguments[1]; | |
return ajaxData.push(ajax_caller(obj)); | |
}); | |
$.when.all(ajaxData).done(function(data){ | |
console.log(data,'done'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment