简体   繁体   中英

Repeat Jquery .ajax a certain number of times with a pause until request is done

I'm wondering if someone can help me write this JavaScript . I basically need a for loop that does a $.ajax request 100 times in a row, but only advances to the next iteration when the $.ajax request is .done()

I think you can try recursion and send the request one by one.

 var count = 0;     
 var recursiveAjax = function() {
     if(count < 100) {
          $.ajax({
              url: 'url here',
              data: {/*data here*/},
              success: function(msg) {
                  count++;
                  recursiveAjax();
              }
          });
      }
      // next
 }
 recursiveAjax();
 var count=0;

function someFunc(somevar) {

var myAjax = $.ajax({
    url: 'index.php',
    type: 'post',
    data: 'somevar=' + somevar,
    dataType: 'json'
});

return myAjax;
}
 while(count<100){
 var test = someFunc(somevar);
 test.done(function (data) {
 count++;

});
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM