navigator.onLine 在我的手机上无法使用。如何检查互联网是否在线。离线???电话间隙

2023-11-21

我正在为我的应用程序使用phonegap。我的应用程序基本上用于来自一个网站的 RSS 提要。 但我的要求是当没有互联网时应用程序应该alert-offline.

当应用程序在线运行时,所有数据都存储到数据库中。 当互联网不存在时,数据会从数据库获取,所以我的问题只是应用程序在使用时每次都显示我在线navigator.onLine.

if(navigator.onLine) {
    alert("check online");
    loadScript("http://www.google.com/jsapi",msgOnFn)
    loadScript("js/googleapi.js", msgOnFn);
} else {
    loadScript("js/db.js",msgOffFn);
}

I tried 这也是在此显示none or wifi每次。我不明白为什么会发生这种情况。

在网络浏览器中,它工作正常。 :)


请遵循音位间隙文档.

您还可以更改给定函数以返回 true 或 false:

function checkConnection() {
    var networkState = navigator.connection.type;

    var states = {};
    states[Connection.UNKNOWN]  = false;
    states[Connection.ETHERNET] = true;
    states[Connection.WIFI]     = true;
    states[Connection.CELL_2G]  = true;
    states[Connection.CELL_3G]  = true;
    states[Connection.CELL_4G]  = true;
    states[Connection.CELL]     = true;
    states[Connection.NONE]     = false;
    if (states[networkState]) {
        return true;
    } else {
        return false;
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

navigator.onLine 在我的手机上无法使用。如何检查互联网是否在线。离线???电话间隙 的相关文章

随机推荐