javascript - jQuery for loop on ajax call and wait until all is done -
he have problem need send multiple ajax calls same url different data. need send in chunks , wait until requests done before _.each function.
i did simple chunk function slice array , slip groups.
my code:
---- js ---- batch = [0,1,2,3,4,5,....] // big array var xhr = chunk(batch, 20); (i=0; < xhr.length ; i++ ) { //loop number of time of xhr.length ajax.call("/", "post", {batch: xhr[i]}).done(function(resp){ arrayresp.push(resp); }); } /// after ajax calls , arrayresp done exectue _.each(arrayresp, function (element) { //do somting }); ----- /js -----
i need in end full array holds resp data
i can't $.when() because can't name function , didn't figure out how use $.deferred() in function can me?
thanks!
var res = []; // may not needed var arrayresp = []; batch = [0,1,2,3,4,5,....] // big array var xhr = chunk(batch, 20); (i=0; < xhr.length ; i++ ) { //loop number of time of xhr.length // push `ajax` jquery promise `res` res.push( ajax.call("/", "post", {batch: xhr[i]}) // may not needed, // see `arguments` @ `.then` .done(function(resp){ arrayresp.push(resp); }) ); } // when `xhr` complete , // process array of `xhr` jquery promises $.when.apply($, res) .then(function() { // stuff `arrayresp`` console.log(arrayresp, arguments); });
Comments
Post a Comment