jquery - GET request inside of a loop in JavaScript -


so, code looks

for(int n = 0; n < object.length; n++){   /*other code */   $.get(...,function(data){     //do stuff   });} 

now, other code executes multiple times should. however, when command ran, ran once , when n reaches object.length. causes sorts of errors. n being incremented in loop.

can not loop get/post commands? or if can, doing wrong? thanks.

the for-loop won't wait $.get call finish need add async flow control around this.

check out async.eachseries. done callback below key controlling loop. after success/fail of each $.get request call done(); (or if there's error, call done(someerr);). advance array iterator , move next item in loop.

var async = require("async"); var list = ["foo", "bar", "qux"];  async.eachseries(list, function(item, done) {    // perform request each item in list   // url?somevar=foo   // url?somevar=bar   // url?somevar=qux   $.get(url, {somevar: item}, function(data) {     // stuff, call done     done();   });  }, function(err) {   if (err) {     throw err;   }   console.log("all requests done"); }); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -