javascript 如何检查设备是在线还是离线 Phonegap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17833542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:48:21  来源:igfitidea点击:

How to check device is Online or Offline Phonegap

javascriptjquerycordovaoffline

提问by Erdem Güng?r

What is the easiest way to check whether the device(smartphone) is online or offline. I'm working with phonegap, jquery mobile. I found this one.

检查设备(智能手机)是在线还是离线的最简单方法是什么。我正在使用 phonegap、jquery mobile。我找到了这个。

document.addEventListener("online", yourCallbackFunction, false);

What I want to do is to check the Internet connection and decide about to get the data from the internet or from the device local database.

我想要做的是检查 Internet 连接并决定是从 Internet 还是从设备本地数据库中获取数据。

if(deviceoffline)
  getDataFromLokalDatabase();
else
  getDataFromInternet();

回答by Someoneinthe

This is the code using native phonegap code:

这是使用本机 phonegap 代码的代码:

function checkInternet() {

    var networkState = navigator.connection.type;

    if(networkState == Connection.NONE) {

        onConnexionError();
        return false;

    } else {

       return true;
    }
}

回答by andlrc

You can use navigator.onLine:

您可以使用navigator.onLine

var isOffline = 'onLine' in navigator && !navigator.onLine;

if ( isOffline ) {
    //local db
}
else {
    // internet data
}

But be aware of that if navigator.onLineisn't supported isOfflinewill always be false

但请注意,如果navigator.onLine不受支持isOffline将始终为假

回答by Divesh Salian

Using Jquery make a ajax call and check the error code, If you get error code as 0 the device is offline

使用 Jquery 进行 ajax 调用并检查错误代码,如果错误代码为 0,则设备处于离线状态

$.ajax({
    //your ajax options
    error: function (request) { 
         if (request.status == 0) {
            alert("you're offline");
        }
    }
});

回答by Rayjax

Beware, while navigator.onLine is the simplest solution, it does not behave consistently on all browsers. On Chrome it will tell you that it is online if you have a LAN cable in, even if you have no internet.

请注意,虽然 navigator.onLine 是最简单的解决方案,但它在所有浏览器上的行为并不一致。在 Chrome 上,如果您有 LAN 电缆,它会告诉您它在线,即使您没有互联网。

You can use the cordova plugin network-information

您可以使用cordova插件网络信息

You can also try to make an AJAX request to your server; usually you want to know if you are offline because in that case you cannot communicate with the server, so "offline" often means "not able to communicate with the server" (which can be also caused by your server being offline). Playing with timeouts or several requests is also useful if you need to check bandwidth or link quality.

您也可以尝试向您的服务器发出 AJAX 请求;通常您想知道您是否处于离线状态,因为在这种情况下您无法与服务器通信,因此“离线”通常意味着“无法与服务器通信”(这也可能是由于您的服务器离线造成的)。如果您需要检查带宽或链接质量,使用超时或多个请求也很有用。

Offline does not mean the same for every use case, you first need to know which of the above techniques is best suited for you, then implement one or several of them.

离线并不意味着每个用例都相同,您首先需要知道上述哪种技术最适合您,然后实施其中的一种或几种。

What I want to do is to check the Internet connection and decide about to get the data from the internet or from the device local database.

我想要做的是检查 Internet 连接并决定是从 Internet 还是从设备本地数据库中获取数据。

It seems that checking the link with your server through a request is the best option for you.

通过请求检查与服务器的链接似乎是您的最佳选择。

回答by Patrik Wallin

the native onLine is somewhat shaky, especially in an web app environment.. but can be used for an initial check upon pageLoad, however it is somewhat useless then as if you get to that point you have an internet connection.

本机 onLine 有点不稳定,尤其是在 Web 应用程序环境中.. 但可以用于对 pageLoad 进行初始检查,但是它有点没用,就好像你到了那个点你有互联网连接一样。

attempting to ajax something is far more reliable.

尝试 ajax 的东西要可靠得多。