2013-09-25 49 views

回答

2

你可以使用setTimeout()設置的最大時間的請求:

var timeout; 
var xhr = $.ajax({ 
    type: "GET", 
    url: "http://myservice.com/jsonp", 
    data: data, 
    success: function(msg){ 
     // All went OK 
     if(timeout != null) clearTimeout(timeout); 
    } 
}); 

timeout = setTimeout(function() { 
    // Request took >= 10 seconds 
    // We could kill it? 
    xhr.abort(); 
    // Or send a message to the user and ask whether they want to continue 
    if(!confirm("Network connection is taking longer than usual to complete. Continue waiting?")) { 
     xhr.abort(); 
    } 
}, 10000); 
+1

'$ .ajax'已經有一個'timeout'選項,另外這不回答這個問題的。 – Prinzhorn

+0

有關超時選項的好處。它肯定回答了這個問題:*但是當連接速度很慢時(幾乎不存在)如何使用任何jquery *檢查這種情況。如果連接時間超過10秒,我會將其定義爲緩慢。 – CodingIntrigue

+0

想長時間輪詢。另外:他(和我)在提出請求之前想知道連接是否緩慢。如果上傳時間超過10秒,我不會說連接速度很慢。 – Prinzhorn

相關問題