Pausing a for each loop awaiting completion of ajax.done

I repeat the array of command files using for each method.

With each batch file, I need to wait for ajax to succeed before continuing with the next command.

The problem is that for each cycle, moving to the next command before completing the ajax code. Can anyone suggest a solution?

For each cycle:

$.each(cmd_files, function(index, cmd) {

          update_log('Running CMD for' + cmd)

          wait_for_cmd_complete(cmd).done(function(data){

               update_log("CMD is complete");    

          })
 })

Ajax Function:

function wait_for_cmd_complete(cmd){

     return $.ajax({
         type: 'POST',
         data: {cmd:cmd}, 
         url: 'wait_for_cmd_complete.php'
     });  


  }
+4
source share
4 answers

It's just not how you write event driven actions. If you need the next iteration of the code just to run after the event, you won’t go through the iteration ... since that would run all the code before the event! This is how events work.

1 :

var i = 0; // the index we're using
var list = []; // array of the things you plan on "looping" through
var max = list.length; // basically how many iterations to do

function nextIteration() {
    if (i >= max) return; // end it if it done
    // do whatever you want done before the event for this iteration
    list[i].addEventListener("someevent", onEvent); // add whatever event listener
}

function onEvent() {
    // do whatever it is you want done after the event for this iteration
    i++; // up the index
    nextIteration(); // start the next iteration
}

nextIteration(); // start the first iteration manually

, , , .

var i = 0; // the index we're using
update_log('Running CMDs');
var cmd; // basically a var just so we don't have to keep calling cmd_files[i]
var totalCommands = cmd_files.length; // basically how many iterations to do

function sendNextCommand() {
    if (i >= totalCommands) return; // end it if it done
    cmd = cmd_files[i]; // again, just so we don't have to keep calling cmd_files[i]
    update_log("Waiting for CMD " + cmd + " to complete...");
    $.ajax({type:'POST', data:{cmd:cmd}, url:'wait_for_cmd_complete.php'}).done(onCommandComplete);
    // above line does what needs to be done (sends to PHP) and then adds the event listener 'done'
}

function onCommandComplete(value) {
    update_log( "    CMD complete for: " + cmd);
    i++; // up the index
    sendNextCommand(); // start the next iteration
}

sendNextCommand(); // start the first iteration manually
+1

.

  • Ajax .

  • cmd ++.

  • Ajax .

  • , ,

  • else cmd.

     number_of_cmd_files = cmd_files.length;
     update_log('Running CMDs')
     i=0;
     cmd= cmd_files[i];
    
     wait_for_cmd_complete(cmd)
    
     function wait_for_cmd_complete(cmd){
    
        update_log("Waiting for CMD " + cmd + " to complete...")
    
       $.ajax({
            type: 'POST',
            data: {cmd:cmd}, 
            url: 'wait_for_cmd_complete.php'
    
        }).done(
             function(value) {
             i++;  
             update_log( "    CMD complete for: " + cmd);
             if (i < number_of_cmd_files) {
               cmd = cmd_files[i]
               wait_for_cmd_complete(cmd);  
             }
           }
        ); 
    

    }

+1

, . , , :

$.each(cmd_files, function(index, cmd) {
            update_log('Running CMD for' + cmd);
            var request =  $.ajax({type: 'POST',data: {cmd:cmd}, url: 'wait_for_cmd_complete.php'});
            request.then( update_log("CMD is complete");        
 });
0

ajax , , . async: false ajax. . .

0
source

Source: https://habr.com/ru/post/1608858/


All Articles