2015-09-24 103 views
4

我使用for each方法遍歷一組命令文件。在等待ajax.done完成每個循環時暫停a

對於每個命令文件,在繼續下一個命令之前,我需要等待ajax成功。

問題是,每個循環在ajax代碼完成之前移動到下一個命令。誰能提供解決方案?

對於每一個循環:

$.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 wait_for_cmd_complete(cmd){ 

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


    } 
+0

使用它,如果Ajax請求sucessfuly完成......這是......在AJAX功能的成功一部分,insted的update_log中的(「CMD完成」);在循環回聲它在阿賈克斯成功,,,, – Akshay

回答

1

這根本不是你如何編寫事件驅動的操作。如果您需要下一代代碼才能在事件之後啓動,那麼您不會循環迭代...因爲這將在事件之前運行所有代碼!這就是事件的工作方式。

使這樣的事情一般結構將更好地爲運行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'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 

爲了便於說明,這樣就可以知道這是怎麼回事,這裏是你的格式如下代碼我以上代碼。

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 
+0

我在這裏看到的唯一區別是你正在使用另一個函數來調用主函數。我的代碼在沒有額外功能的情況下做同樣的事情。直到響應從ajax請求返回時,下一段代碼纔會啓動。我在編程方面是一個相對的菜鳥,所以我很可能是錯的。 – user3101337

+0

@ user3101337 - 你也爲它做了一個函數,你只是使用一個匿名函數而不是給它一個名字(它在你調用done()的內部),並且你正在重建一個匿名函數的實例時間wait_for_cmd_complete被調用而不是一次,只是重複調用它。 –

+0

除此之外,我的代碼和你的代碼之間的主要區別在於我的意圖是通用,並且容易閱讀/理解到底發生了什麼。你的代碼回答了這個問題,因爲它是解決問題的代碼。礦是相同的一般概念,但用一種更容易教導正在發生的事情以及爲什麼其他人可以將其應用於自己的問題的方式來回答問題。這也不是jQuery相關的代碼,它只是JS。我也可以用同樣的結構完成jQuery事件。 –

0

也許嘗試鏈接您的活動。不太熟悉這種方法,但我認爲這將工作:

$.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尚未完成,請求仍會發生。我現在有同樣的問題。 – user3101337

1

這就是我最終開始工作。

  1. 第一個Ajax請求已完成。

  2. cmd文件數組使用i ++進階。

  3. 從同一個函數中再次調用Ajax函數。

  4. 如果有更多的文件仍然運行,函數被再次調用,

  5. 其他功能後退出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); 
         } 
         } 
        ); 
    

    }

0

使用同步AJAX應該工作,至少如果你不這樣做跨域。 將async: false添加到ajax選項。 有關更多信息,請參閱herehere

+1

異步選項已在最新版本的jquery中被棄用。 它可能會給最終用戶帶來不好的體驗,最好不要使用。 – user3101337

+0

是的,你是對的,不知道。我所有的jQuery知識基本上都在1.8之前。所以如果你不能這樣做,你提出的解決方案可能是最好的 – patrick96