Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在不刷新整个页面的情况下检查internet连接_Javascript_Refresh - Fatal编程技术网

Javascript 如何在不刷新整个页面的情况下检查internet连接

Javascript 如何在不刷新整个页面的情况下检查internet连接,javascript,refresh,Javascript,Refresh,我希望每x秒调用一次以下函数,这样我就不必刷新页面 var rq = new XMLHttpRequest(); rq.open('GET', "SAME DOMAIN ADDRESS", true); rq.onreadystatechange = function() { if(rq.readyState === 4) { if(rq.status === 200) { clearTimeout(xmlHttpTimeout); window.locat

我希望每x秒调用一次以下函数,这样我就不必刷新页面

var rq = new XMLHttpRequest();
rq.open('GET', "SAME DOMAIN ADDRESS", true);
rq.onreadystatechange = function() {
    if(rq.readyState === 4) {
        if(rq.status === 200) {
      clearTimeout(xmlHttpTimeout); 
window.location.href = "Tracker.html"; // if internet connection found, redirect.
        } else {
        }
    }
};
rq.send(""); 
var xmlHttpTimeout=setTimeout(ajaxTimeout,5000);
function ajaxTimeout(){
   rq.abort();
// IF no internet connection found, call this whole javascript function/code AGAIN in 5 seconds! to check for internet connection
}

基本上,我希望在不刷新整个页面的情况下检查internet连接,如果有,则重定向到
Tracker.html

最佳方法是使用
setTimeout

function callMe() {
  // Some Code
  setTimeout(callMe, 1000);
}

callMe();
您的代码如下所示:

var rq = new XMLHttpRequest();

rq.onreadystatechange = function() {
  if(rq.readyState === 4) {
    if(rq.status === 200) {
      window.location.href = "Tracker.html"; // if internet connection found, redirect.
    } else {
      setTimeout(rq.send, 5000);
    }
  }
};

rq.send();

如果要检查客户端是否已连接,还可以使用所述的新的联机和脱机事件。

检查
navigator.online
是否正确。

ajax解决方案将仅在同一域上工作。但是,在同一个域上,您可能不需要测试服务器是否联机。使用navigator.onLine,它将指示是否可以访问internet,但仍然无法访问相应的服务器

诀窍是尝试加载目标服务器的映像,如果出现错误,请在5秒钟后重试。当映像成功加载后,可以执行重定向

<html>
  <header>
  </header>
  <body>
    <script type="text/javascript">
    var img = new Image();
    var ping = function() { img.src = "http://www.somedomain.com/valid_image.jpg"; }
    ping();

    img.onerror = function(e) {
      console.log('error');
      setTimeout(function() {
        ping();
      }, 5000);
    }

    img.onload = function(e) {
      console.log('YES');
      window.location.href = "http://www.google.com"; 
    }
    </script>

  </body>
</html>

var img=新图像();
var ping=function(){img.src=”http://www.somedomain.com/valid_image.jpg"; }
ping();
img.onerror=函数(e){
console.log('error');
setTimeout(函数(){
ping();
}, 5000);
}
img.onload=函数(e){
console.log('YES');
window.location.href=”http://www.google.com"; 
}

AJAX-愿原力与你同在!。当然,当你知道答案时,实际答案可能已经改变了。也就是说,连接可能会随机上下移动,因此重定向可能仍然失败。所以问题是……如何将上面的javascript代码“插入”到callMe函数中?我这样问是因为,上面的代码中有更多的函数……我已经编辑了我的代码,但我强烈建议您使用navigator.onLine,除非您不必支持旧浏览器。哇!这是强大的,从未听说过。谢谢!