javascript 如何定期检查Phonegap中的互联网连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17377579/
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
How to check internet connection in Phonegap periodically?
提问by zevio rodrigues
I am working with Phonegap. I need to check the network connection periodically. Actually I am getting some data from a server. If there is no connection, I need to show an error alert.
我正在与 Phonegap 合作。我需要定期检查网络连接。实际上我正在从服务器获取一些数据。如果没有连接,我需要显示错误警报。
I googled it and found the solution. But it is not ok. Because I need to check the connection periodically.
我用谷歌搜索并找到了解决方案。但这不行。因为我需要定期检查连接。
<html>
<head>
<title>navigator.network.connection.type Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
checkConnection();
}
function checkConnection() {
var networkState = navigator.network.connection.type;
var states = {};
states[Connection.UNKNOWN] = 'Unknown connection';
states[Connection.ETHERNET] = 'Ethernet connection';
states[Connection.WIFI] = 'WiFi connection';
states[Connection.CELL_2G] = 'Cell 2G connection';
states[Connection.CELL_3G] = 'Cell 3G connection';
states[Connection.CELL_4G] = 'Cell 4G connection';
states[Connection.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
}
</script>
</head>
<body>
<p>A dialog box will report the network state.</p>
</body>
</html>
It check only the first time when the application launches. But I need to check it periodically, because I am doing socket programming. If there is a problem with internet, I need to show it. But this code only shows at launch time.
它仅在应用程序启动时第一次检查。但是我需要定期检查它,因为我在做套接字编程。如果互联网有问题,我需要显示它。但此代码仅在启动时显示。
回答by Gajotres
Working example: http://jsfiddle.net/Gajotres/d5XYR/
工作示例:http: //jsfiddle.net/Gajotres/d5XYR/
Use interval timer to check internet connection every predefined time. This solution requires HTML5 browser but this is not a problem because jQuery Mobile already requires HTML5 browser. In this case timer will check internet connection every 100 ms and set final result into a javascript global variable.
使用间隔计时器在每个预定义的时间检查互联网连接。此解决方案需要 HTML5 浏览器,但这不是问题,因为 jQuery Mobile 已经需要 HTML5 浏览器。在这种情况下,计时器将每 100 毫秒检查一次互联网连接,并将最终结果设置为 javascript 全局变量。
Everything depends on this line:
一切都取决于这一行:
window.navigator.onLine -- it will be false if the user is offline.
Final solution:
最终解决方案:
var connectionStatus = false;
$(document).on('pagebeforeshow', '#index', function () {
setInterval(function () {
connectionStatus = navigator.onLine ? 'online' : 'offline';
}, 100);
$(document).on('click', '#check-connection', function () {
alert(connectionStatus);
});
});
Tested on:
测试:
Windows Firefox
Windows Google Chrome
Windows IE9 and IE10
Android 4.1.1 Chrome
iPad 3 Safari
iPad3 Chrome
火狐浏览器
视窗谷歌浏览器
视窗 IE9 和 IE10
安卓 4.1.1 铬
iPad 3 Safari
iPad3 铬
回答by Matty J
Perhaps you could use the 'online' and 'offline' event listeners on the document to notify your App when it becomes online or offline, as described in the docs eg. here: http://docs.phonegap.com/en/3.2.0/cordova_events_events.md.html#online
也许您可以使用文档上的 'online' 和 'offline' 事件侦听器在它变为在线或离线时通知您的应用程序,如文档中所述,例如。在这里:http: //docs.phonegap.com/en/3.2.0/cordova_events_events.md.html#online
回答by christianmenkens
Unfortunately the answer is not stable on Chrome and Safari as well as several mobile browsers. It does not give you the "real Internet connection" state but rather the "network connection state". See:
不幸的是,Chrome 和 Safari 以及一些移动浏览器上的答案并不稳定。它不会为您提供“真正的 Internet 连接”状态,而是“网络连接状态”。看:
https://developer.mozilla.org/en-US/docs/Web/API/window.navigator.onLine
https://developer.mozilla.org/en-US/docs/Web/API/window.navigator.onLine
"In Chrome and Safari, if the browser is not able to connect to a local area network (LAN) or a router, it is offline; all other conditions return true. So while you can assume that the browser is offline when it returns a false value, you cannot assume that a true value necessarily means that the browser can access the internet. You could be getting false positives, such as in cases where the computer is running a virtualization software that has virtual ethernet adapters that are always "connected." Therefore, if you really want to determine the online status of the browser, you should develop additional means for checking. To learn more, see the HTML5 Rocks article, Working Off the Grid."
“在 Chrome 和 Safari 中,如果浏览器无法连接到局域网 (LAN) 或路由器,则它处于离线状态;所有其他条件都返回 true。因此,虽然您可以假设浏览器在返回假值,您不能假设真值一定意味着浏览器可以访问互联网。您可能会得到误报,例如在计算机运行虚拟化软件的情况下,该软件具有始终“连接”的虚拟以太网适配器。 ” 因此,如果您真的想确定浏览器的在线状态,您应该开发额外的检查方法。要了解更多信息,请参阅 HTML5 Rocks 文章“Working Off the Grid”。
See this HTML5 Rocks article for a true stable solution - more work of course:
请参阅这篇 HTML5 Rocks 文章以获得真正稳定的解决方案 - 当然还有更多工作:
回答by Ramprasath Selvam
Try this code,
试试这个代码,
<script>
$(document).ready(function () {
document.addEventListener("deviceready", onDeviceReady, false);
document.addEventListener("online", onOnline, false);
document.addEventListener("offline", onOffline, false);
function onDeviceReady() {
//onDeviceReaddy
}
function onOnline() {
//onOnline
}
function onOffline() {
navigator.notification.alert(
'Please Check your internet connection.', // message
null, // callback
'AllCare Doctor', // title
'OK' // buttonName
);
}
</script>
For More Details Click Here..!
更多详情请点击这里..!