简体   繁体   English

JS离线检测? 我究竟做错了什么

[英]JS Detect Offline? What Am I doing wrong

I'm a JS newb so I'm trying to figure out how to make this example from w3c to work in my WebApp that will run in a PhoneGap framework... 我是JS newb,所以我想弄清楚如何从w3c制作此示例以在将在PhoneGap框架中运行的WebApp中工作...

I'm sure this is easy, but when the event listener is triggered, it runs the attached function with the alert, problem is is it proceeds to excute everything after that again. 我敢肯定这很简单,但是当事件监听器被触发时,它会运行带有警报的附加功能,问题是它会继续执行所有操作。 So say I flip my iphone off, I'll get an alert then it will execute the next alert which says its online... Anywho... If any of you gys have any better ideas about executing a function like this let me know PhoneGap Cordova has two other methods as well. 所以说我关掉iphone,我会得到一个警报,然后它将执行下一个警报,该警报在网上说...任何人...如果你们gys对执行这样的功能有更好的主意,请告诉我PhoneGap Cordova还有另外两种方法。

<script>
<!--
window.addEventListener("offline", function(e) {alert("offline");})
window.addEventListener("online", function(e) {alert("online");})
if (navigator.onLine) {
  alert('online')
  //functions to run online
} else {
  alert('offline');
  //offline functions or through into loop
}
-->
</script>

You are missing a parameter in the addEventListener function. 您缺少addEventListener函数中的参数。 For me this is working fine: 对我来说,这很好用:

window.addEventListener("offline", function () {
    console.log("Online status: " + navigator.onLine);
}, false);

window.addEventListener("online", function () {
    console.log("Online status: " + navigator.onLine);
}, false);

试试这个if(window.navigator.onLine) { alert('I am online');}我刚刚在控制台中对其进行了测试,它可以正常工作:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM