He I have a problem I need to send multiple AJAX calls to the same URL but with different data. I need to send it in chunks and wait until all requests are done before my _.each function.
i did a simple chunk function that slice my array and slip it to groups.
my code:
---- js ----
batch = [0,1,2,3,4,5,....] // big array
var xhr = chunk(batch, 20);
for (i=0; i < xhr.length ; i++ ) {
//loop number of time of xhr.length
ajax.call("/", "POST", {batch: xhr[i]}).done(function(resp){
arrayResp.push(resp);
});
}
/// after all ajax calls and arrayResp is done
exectue
_.each(arrayResp, function (element) {
//do somting
});
----- /js -----
i need to get in the end a full array that holds all my resp data
I can't do $.when() because I can't name the function And I didn't figure out how to use the $.Deferred() in this function Can you help me?
Thanks!
var res = [];
// this may not be needed
var arrayResp = [];
batch = [0,1,2,3,4,5,....] // big array
var xhr = chunk(batch, 20);
for (i=0; i < xhr.length ; i++ ) {
//loop number of time of xhr.length
// push `ajax` jQuery promise to `res`
res.push(
ajax.call("/", "POST", {batch: xhr[i]})
// this may not be needed,
// see `arguments` at `.then`
.done(function(resp){
arrayResp.push(resp);
})
);
}
// when all `xhr` complete ,
// process array of `xhr` jQuery promises
$.when.apply($, res)
.then(function() {
// do stuff with `arrayResp``
console.log(arrayResp, arguments);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.