How to make for loop wait until Async call was successful before to continue

后端 未结 1 1884
一整个雨季
一整个雨季 2020-12-10 19:08

Hi i am creating a batch update for my local store using for loop and async ajax call.

My problem is that my loop continues even though my ajax call still not yet su

1条回答
  •  囚心锁ツ
    2020-12-10 19:32

    Please don't use "for loop". Instead in the success callback increase the counter and re-trigger itself. something like below.

    function mySyncFunction (counter, totRecords){
       if(counter === undefined) 
         counter = 0;   
       if(counter >=totRecords) return;
    
       var defectssurl = 'https://test.com/mywcf.svc/GetListAllPaging?id=' + counter;
       Ext.Ajax.request({
         url:defectssurl,
            // snip // your code here
         success: function (resp) {
            // snip // your code here
            counter++;
            mySyncFunction(counter, totRecords);
         }
         // snip // your code here
    });
    

    0 讨论(0)
提交回复
热议问题