简体   繁体   中英

how to execute second loop iteration after ajax done

how to delay for loop until function "some_multi_ajax_function()" change global variable (exe_counter will be 0) ?

for (i = 0; i < some_data_list.length; i++) {
    exe_counter=1;
     if(i>0 && exe_counter != 0){
            delay{
                // delay for loop until some_multi_ajax_function() set exe_counter = 0
                // do not execute second iteration
            }
      }else{
            data = some_data_list[i];
           // Many ajax posts will be executed here. In the end exe_counter will be set to 0;
           some_multi_ajax_function(data);
      }
}

function some_multi_ajax_function(data){
    $.ajax({
            ...
      }.done(function(d) {
           // here it could be used another ajax function 
           exe_counter = 0;
      });
}

You take the problem with a wrong solution.

If you want to wait an Ajax action you have to use callback only.

Then recursively you should do :

function some_multi_ajax_function(data, i) {
    $.ajax({
        ...
    }.done(function(d) {
         if (i < data.length) {
             some_multi_ajax_function(data, i + 1);
         } else {
             // Do something in the end
         }
    });
}

some_multi_ajax_function(some_data_list, 0);

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