【问题标题】:Check device internet connection every minute with jquery mobile使用 jquery mobile 每分钟检查一次设备互联网连接
【发布时间】:2015-06-16 10:09:54
【问题描述】:

每创建一页就会检查一次 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);    

    });

【问题讨论】:

  • 你可以检查这个answer 否则
  • sonavigation.online 不可信
  • 我会说它有很多限制..

标签: javascript jquery ajax jquery-mobile


【解决方案1】:
  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;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    • 1970-01-01
    相关资源
    最近更新 更多