javascript - adding new objects to array within a loop with a delay -


i have for loop needs make ajax call 1 second delay between iterations. should grab object , .push onto array. code below adds first object. doing wrong?

var maxloops = 10; var counter = 0;  (function processpages() {     if(counter++ >= maxloops) return;      settimeout(function() {         //check page count , loop. push new objects allproducts         (var i=1; <= totalpages; i++){             $.ajax({ url: '/process.php',                 data: {category: 'sportinggoods', page: i},                 type: 'post',                 success: function(output) {                     allproducts.push(output);                 }             })         }         }), 1000; })(); 

if want few ajax calls , wait between them, can this:

(function processpages(i) {     if (i===totalpages) {          // pages have been fetched, may          // use allproducts array here          return;     }     $.ajax({ url: '/process.php',         data: {category: 'sportinggoods', page: i},         type: 'post',         success: function(output) {             allproducts.push(output);             settimeout(processpages, 1000, i+1);         }     }) })(0); 

this stop when i totalpages.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -