2011-11-03 76 views
0

我正在寫一個小型的網絡應用程序,並希望在允許他提交他的數據之前檢查用戶連接。如何檢查用戶連接?

 function testConnection() { 
     var xmlhttp; 

     document.getElementById("checkingConnectivity").style.display = "block"; 

     if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
     } else {      // code for IE6, IE5 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 

     var success = false; 

     xmlhttp.onreadystatechange = function() { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
     document.getElementById("noConnectivity").style.display = "none"; 
     document.getElementById("checkingConnectivity").style.display = "none"; 
     success = true; 
     } 
     } 

     connIterator = connIterator + 1; 
     xmlhttp.open("GET","testConnection.jsp?i="+connIterator,true); // Dynamic URL to prevent caching 
     xmlhttp.send(); 

     // Wait 10 seconds 
     for(var i = 0; i < 10; i++) { 
     // If no success so far, keep waiting 
     if(!success) { 
     window.setTimeout('1000'); 
     } else { 
     return true; 
     } 
     } 

     // success still isn't true, so we assume a timeout 
     document.getElementById("noConnectivity").style.display = "block"; 
     document.getElementById("checkingConnectivity").style.display = "none"; 
     return false; 
     } 

的問題是,這個函數總是返回false,即使該文件是可到達。 在window.setTimeout('1000');之前添加警報時,所以我認爲setTimeout不起作用。

你有什麼建議嗎?

在此先感謝!

+0

爲什麼你甚至在開發webapp時創建連接檢查? – Jordy

+0

在我的情況下,用戶可能會不時斷開連接 – Gundon

+0

這就是爲什麼我們有HTTP狀態代碼; p – Jordy

回答

1

在檢查完成之前,您的函數正在返回。請記住,JavaScript是事件驅動的。這裏是一個潛在的修復(未經測試):

// when connected, call the callback 
function testConnection(callback) { 
var xmlhttp; 

document.getElementById("checkingConnectivity").style.display = "block"; 

if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
} else {      // code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 

xmlhttp.onreadystatechange = function() { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
    document.getElementById("checkingConnectivity").style.display = "none"; 
    if (callback) { 
     callback(xmlhttp); 
    } 
    } 
} 

connIterator = connIterator + 1; 
xmlhttp.open("GET","testConnection.jsp?i="+connIterator,true); // Dynamic URL to prevent caching 
xmlhttp.send(); 
} 


document.getElementById("noConnectivity").style.display = "block"; 
document.getElementById("checkingConnectivity").style.display = "none"; 
testConnection(function() { 
    // this code will run when the connection succeeds 
    document.getElementById("noConnectivity").style.display = "none"; 
}); 
+0

感謝您的幫助!但是這不會消除請求剛剛超時的可能性? – Gundon

+0

在這段代碼中,noConnectivity將保留在屏幕上,直到請求成功。但是當xmlhttp對象經歷不同的狀態時,你當然可以做各種各樣的事情。 –

+0

謝謝!我現在會打勾你的答案,但我發現這個http://stackoverflow.com/questions/1018705/how-to-detect-timeout-on-an-ajax-xmlhttprequest-call-in-the-browser確實解決了我的問題也是問題。你認爲我應該把我的問題標記爲重複嗎? – Gundon

0

navigator.onLine。規範說:

如果用戶代理肯定離線(從網絡斷開連接 ),則返回false。如果用戶代理可能在線,則返回true。

當此 屬性的值更改時,會觸發事件在線和離線。

的navigator.onLine屬性必須返回false,如果用戶遵循鏈接或當 腳本請求遠程頁面(或者知道這種嘗試會失敗 ),並且必須在用戶代理 不會接觸網絡否則返回true。

問題它不是可靠的,因爲電腦可能有網絡連接,但沒有互聯網接入。