Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.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 使用jquery mobile每分钟检查一次设备互联网连接_Javascript_Jquery_Ajax_Jquery Mobile - Fatal编程技术网

Javascript 使用jquery mobile每分钟检查一次设备互联网连接

Javascript 使用jquery mobile每分钟检查一次设备互联网连接,javascript,jquery,ajax,jquery-mobile,Javascript,Jquery,Ajax,Jquery Mobile,每创建一个页面,都会检查Internet连接。但是,当设备上的wifi关闭时,页面将重定向到no_internet.hmtl页面。我们如何在每次打开/关闭wifi按钮时检查互联网连接?页面应重定向至no_internet.html $(document).on('pagebeforecreate', '[data-role="page"]', function(){ //loading spinner setInterval( function(){

每创建一个页面,都会检查Internet连接。但是,当设备上的wifi关闭时,页面将重定向到no_internet.hmtl页面。我们如何在每次打开/关闭wifi按钮时检查互联网连接?页面应重定向至no_internet.html

$(document).on('pagebeforecreate', '[data-role="page"]', function(){      //loading spinner

    setInterval(
        function(){


          if(window.navigator.onLine){ 

            }else {

                 window.location='./no_internet.html';

                 return false;

            }

     }, 1000);

        setTimeout(function(){

         $.mobile.loading('show', {
        text: 'Chargement en cours...',
        textVisible: true,
        theme: 'a',
        html: "<span class='ui-bar ui-overlay-c ui-corner-all' ><img width='50px' height='50px' src='http://www.shougun.it/images/loading.gif' /><br><h2>Chargement en cours...</h2></span>"
    });
        },5);    

    });
$(文档)。在('pagebeforecreate','[data role=“page”]'上,函数(){//加载微调器
设定间隔(
函数(){
if(window.navigator.onLine){
}否则{
window.location='./no_internet.html';
返回false;
}
}, 1000);
setTimeout(函数(){
$.mobile.loading('show'{
文字:“按课程收费……”,
textVisible:对,
主题:"a",,
html:
按课程收费 }); },5); });
var interval=setInterval(函数(){
connectionExist();//如果存在internet连接,则函数返回true
}, 1000);
函数connectionExist(){
var xhr=new XMLHttpRequest();
变量文件=”http://localhost:50041/stackflow/icon1.png“;//指定的URL是要检查的文件的路径
var randomNum=Math.round(Math.random()*10000);
xhr.open('HEAD',file+“?rand=“+randomNum,false);
试一试{
xhr.send();
如果(xhr.status>=200&&xhr.status<304){
console.log('Connected');
返回true;
}否则{
log('Connection Exist');
间隔时间;
window.location.href=”http://localhost:50041/stackflow/noInternet.html";
返回false;
}
}捕获(e){
返回false;
}
}

您可以通过其他方式检查此选项,因此navigation.online不可信。我会说它有很多限制。。
  var interval=setInterval(function(){ 
        connectionExist(); // function returns a true if an internet connection exists
    }, 1000);

    function connectionExist() {
        var xhr = new XMLHttpRequest();
        var file = "http://localhost:50041/stackflow/icon1.png"; //URL we specify is the path to the file that we want to check on
        var randomNum = Math.round(Math.random() * 10000);

        xhr.open('HEAD', file + "?rand=" + randomNum, false);

        try {
            xhr.send();

            if (xhr.status >= 200 && xhr.status < 304) {
                console.log('Connected');
                return true;
            } else {
                console.log('Connection Exist');
                clearInterval(interval);
                window.location.href = "http://localhost:50041/stackflow/noInternet.html";
                return false;
            }
        } catch (e) {
            return false;
        }
    }