Javascript 如何检查用户的连接?

Javascript 如何检查用户的连接?,javascript,ajax,timeout,xmlhttprequest,Javascript,Ajax,Timeout,Xmlhttprequest,我正在编写一个小web应用程序,希望在我允许他提交数据之前检查用户的连接 function testConnection() { var xmlhttp; document.getElementById("checkingConnectivity").style.display = "block"; if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Ope

我正在编写一个小web应用程序,希望在我允许他提交数据之前检查用户的连接

      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;
      }
函数testConnection(){
var-xmlhttp;
document.getElementById(“checkingConnectivity”).style.display=“block”;
if(window.XMLHttpRequest){//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}else{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
var成功=false;
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“无连接”).style.display=“无”;
document.getElementById(“checkingConnectivity”).style.display=“无”;
成功=真实;
}
}
connIterator=connIterator+1;
open(“GET”、“testConnection.jsp?i=“+connIterator,true);//防止缓存的动态URL
xmlhttp.send();
//等10秒钟
对于(变量i=0;i<10;i++){
//如果到目前为止没有成功,请继续等待
如果(!成功){
setTimeout('1000');
}否则{
返回true;
}
}
//成功仍然不是真的,所以我们假设超时
document.getElementById(“无连接”).style.display=“块”;
document.getElementById(“checkingConnectivity”).style.display=“无”;
返回false;
}
问题是这个函数总是返回false,即使文件是可访问的。 在
window.setTimeout('1000')之前添加警报时它可以工作,所以我假设
setTimeout
不工作

你有什么建议吗


提前谢谢

navigator.onLine
。说明书上说:

如果用户代理确实处于脱机状态(已断开连接),则返回false 来自网络)。如果用户代理可能联机,则返回true

当此值为0时,将触发联机和脱机事件 属性更改

如果用户代理 当用户跟随链接或 脚本请求远程页面(或者知道这样的尝试会 失败),否则必须返回true


问题在于它不是那么可靠,因为计算机可能有网络连接,但没有互联网接入。

在检查完成之前,您的功能将返回。记住,JavaScript是事件驱动的。下面是一个潜在的修复(未经测试):


为什么在开发Web应用程序时还要创建连接检查?在我的情况下,很肯定用户可能会不时失去连接,这就是为什么我们有HTTP状态码;如果服务器根本无法访问,它也不会返回状态码:感谢您的帮助!但这难道不排除请求超时的可能性吗?在该代码中,在请求成功之前,屏幕上不会保留任何连接。但是当
xmlhttp
对象经历不同的状态时,您当然可以做各种事情。谢谢!我现在想勾选你的答案,但我发现这也解决了我的问题。你认为我应该把我的问题标记为重复的吗?把这个问题当作它自己的,因为它已经有了几个答案。我将编辑此问题以添加到另一个问题的链接。
// 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";
});