2017-04-07 53 views
1

在這個片段中,$。阿賈克斯(本)之後,我需要的是使五種Ajax調用,但我需要每個請求之間的五個秒的延遲。請幫忙!重試Ajax調用錯誤5秒

$.ajax({ 
    type: "POST", 
    data: JSON.stringify(download), 
    contentType: "application/json", 
    url: '', 
    retryCount: 1, 
    retryLimit: 5, 
    success: function(data) { 
    $.ajax({ 
     type: "GET", 
     url: '', 
    }) 
     .success(function(data) { 

    }) 
     .error(function(data) { 

    }) 
    }, 
//It has to go into error 
    error : function(xhr, textStatus, errorThrown) { 
    this.retryCount++; 
    if (this.retryCount <= this.retryLimit) { 
     //try again 
     $.ajax(this); 
     return; 
    } else { 
     //user comes here After trying in the if loop for 5 times, with each request having five seconds delay. I am not able to keep delay between each request 
    } 
    return; 
    } 

}); 
+1

您曾經考慮過使用的setTimeout爲你的代碼的延遲方面? –

+0

https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout –

回答

0

試試這個:

error : function(xhr, textStatus, errorThrown) { 
    this.retryCount++; 
    if (this.retryCount <= this.retryLimit) { 
     //try again 
     var ajaxObject = this; 
     window.setTimeout(function(){ 
      $.ajax(ajaxObject); 
     },5000); 

     return; 
    } else { 
     //user comes here After trying in the if loop for 5 times, with each request having five seconds delay. I am not able to keep delay between each request 
    } 
    return; 
    } 
+0

它的工作原理,非常感謝丹尼爾和一個簡單的問題。我有五個錨標記承擔。你知道如何從jQuery的選擇特定的錨 –

+0

$(「時三十分」)$(「a.o」)等 –

+0

我在前面的代碼一個錯誤。如何禁用按鈕,以避免進行另一個新的調用,而這個請求正在進行 –

0

嘗試包裝你的Ajax調用是這樣的:

setTimeout(function() { 
    $.ajax(this); 
}, 5000); 
+0

'function()=' - 一些新的語法? :P –

+0

對不起雅這樣可不行 –

+0

笑對不起我打字的手機,並且必須是一個錯字:)我已經使用了很多箭頭功能最近和可能下滑,在錯誤。 –