That's simply not how you write event driven actions. If you need the next iteration of code to only start after an event then you don't loop through the iterations...since that would run all the code before the event! That's just how events work.
Making something like this general structure would work better for running 1 iteration of code every event:
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's 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
For illustrative purposes so that you can know what's going on, here's your code formatted like my above code.
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's 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