没有互联网连接时的警报框 - Phonegap [英] Alert box when no internet connection - Phonegap
问题描述
我正在尝试在设备上没有互联网连接时弹出一个弹出窗口.
I'm trying to get a pop-up to, well, pop up when there is no internet connection on the device.
我得到 以下示例 工作,但现在我希望仅在结果为无网络连接"时显示警报.
I got the following example working, but now I want the alert only to show when the result is "No network connection".
我试过了:
if (states[Connection.NONE]){
alert('Geen internet :(');
};
但这只会弹出警报框,无论是否有连接.谁能帮我?:)
But that just makes the alert-box pop up, no matter if there is a connection or not. Who can help me? :)
推荐答案
老问题,但这是我的做法 - 您可以使用事件来检测设备是在线还是离线.这非常适合这种情况,因为一旦设备离线,弹出窗口就会出现:
Old-ish question, but here's how I'd do it - You can use events to detect if the device is online or offline. This is ideal for this situation as the pop-up will appear as soon as the device goes offline:
document.addEventListener("offline", function(){ alert("你离线") }, false);
也要做同样的事情,但是当设备重新连接互联网时?:
And to do the same, but when the device regains an internet connection?:
document.addEventListener("online", function(){ alert("你在线") }, false);
在此处查看事件文档:http://docs.phonegap.com/zh/1.8.1/cordova_events_events.md.html#offline
Check out the events docs here: http://docs.phonegap.com/en/1.8.1/cordova_events_events.md.html#offline
更新:
从 cordova 5 开始,这些事件已移至 cordova-plugin-network-information
as of cordova 5 these events have been moved to cordova-plugin-network-information
这篇关于没有互联网连接时的警报框 - Phonegap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!