2014-05-19 111 views
1

在進入下一次迭代之前是否可以讓此循環等待直到AJAX調用完成?讓循環等待ajax調用

for(curr;curr < total;curr++) { 
    $this.val('Uploading '+(curr+1)+' out of '+total); 
    var tmpStr = JSON.stringify(o[curr]).replace("&", "and"); 
    tmp[0] = JSON.parse(tmpStr); 

    $.ajax({ 
     type: 'POST', 
     url: 'http://example.com/api/post', 
     data: "data="+JSON.stringify(tmp), 
     success: function(msg) { 

      var d = JSON.parse(msg); 
      if (d.success) { 

       //localStorage.removeItem("data"); 
      } 
      else 
      { 
       alert("Data failed to upload, please try again"); 
      } 
     }, 
     error: function(msg) { 
      alert("Data failed to upload, please try again"); 
      $this.val('Upload to CMS'); 
     } 
    }); 
} 
+2

迂迴地,沒有。它不是** a ** jax,如果它是同步的,但我建議你重構你的代碼,而不是使其同步 –

回答

2

您可以使用帶遞歸功能的when關鍵字。關鍵字when允許您等待ajax調用。在ajax調用之後,當函數將結果發送到完成函數時,即使它失敗了。您可以通過在ajax調用中添加一些錯誤控制來擴展您的場景。但是我給這個例子:

function reCursive(curr,total) 
{ 
    if(curr < total) 
    { 
     $this.val('Uploading '+(curr+1)+' out of '+total); 
     var tmpStr = JSON.stringify(o[curr]).replace("&", "and"); 
     tmp[0] = JSON.parse(tmpStr); 

     $.when(function() 
     { 
      return $.ajax(
      { 
       type: 'POST', 
       url: 'http://mvc.sivapi.sheffieldnetwork.com/form/submit/submit/meadowhallEaster/', 
       data: "data="+JSON.stringify(tmp), 
       success: function(msg) 
       { 
        var d = JSON.parse(msg); 
        if (d.success) 
        { 
         //localStorage.removeItem("data"); 
        } 
        else 
        { 
         alert("Data failed to upload, please try again"); 
        } 
       }, 
       error: function(msg) 
       { 
        alert("Data failed to upload, please try again"); 
        $this.val('Upload to CMS'); 
       } 
      }); 
     }).done(function (x) 
     { 
      //When you get the result from your ajax call. Write your code here. 
      curr++; 
      reCursive(curr, total); 
     }); 
    } 
} 
+0

收拾格式,你可能會得到一些積分。 –

+0

@LayTaylor這是好嗎? –

+0

+1更好,但使用4個空格作爲製表符(像現在這樣) –

-1

通過將async: false設置爲請求的一部分,可以使ajax請求同步。這有點挫敗了使用Ajax的目的,但它會讓它像普通的同步代碼一樣運行。

+1

噓!嘶! OP要求什麼,但他們要求什麼太爛! – spender

+0

我們可以假裝'async:false'不存在嗎?教他們做他們真正想做的事的其他方法,而不是他們要求的*。 –

+0

如果您的答案是「您可以做到這一點,但不要這樣做」,那麼請不要回答。 –

2

雖然你不能使循環for循環等待,你可以重寫使用調用的另一種風格來運行代碼。

function runOnce(curr) { 
    $this.val('Uploading ' + (curr + 1) + ' out of ' + total); 
    var tmpStr = JSON.stringify(o[curr]).replace("&", "and"); 
    tmp[0] = JSON.parse(tmpStr); 

    $.ajax({ 
    type: 'POST', 
    url: 'http://mvc.sivapi.sheffieldnetwork.com/form/submit/submit/meadowhallEaster/', 
    data: "data=" + JSON.stringify(tmp), 

    // ***ADDED HERE* 
    always: { 
     if (curr < total) { 
     runOnce(curr+1); 
     } else { 
     alert("All done-- you could call your next function from here."); 
     } 
    }, 
    success: function (msg) { 

     var d = JSON.parse(msg); 
     if (d.success) { 
     //localStorage.removeItem("data"); 
     } else { 
     alert("Data failed to upload, please try again"); 
     } 
    }, 
    error: function (msg) { 
     alert("Data failed to upload, please try again"); 
     $this.val('Upload to CMS'); 
    } 
    }); 
} 

// Set things up and run it once. 
// The '0' is what the loop index would be. 
runOnce(0); 
+0

遞歸贏得 –

+0

@JonP - 從技術上講,這是* not *遞歸,因爲在堆棧中只有一個'runOnce'實例... –

+0

獲取方追蹤,但不是函數自己調用的事實作爲「總是」條件的一部分使它遞歸......還是有一些微妙的東西我錯過了? –