JavaScript检测用户是否在线!( 二 )

可以通过如下方式进行调用:
// Hack to use the checkNetwork() function only on Firefox // (http://stackoverflow.com/questions/5698810/detect-firefox-browser-with-jquery/9238538#9238538)// (But it may be too restrictive regarding other browser// who does not properly support online / offline events)if (!(window.mozInnerScreenX == null)) {window.setInterval(checkNetwork, 30000); // Check the network every 30 seconds}使用jQuery监听连接、断开连接事件:
【JavaScript检测用户是否在线!】$(window).bind('online offline', function(e) {if (!IS_ONLINE || !window.navigator.onLine) {alert('We have a situation here');} else {alert('Battlestation connected');}});4.本文总结本文主要和大家介绍如何使用JavaScript检测用户是否在线,主要是通过window.navigator.onLine属性和window的online/offline完成 。文末的参考资料提供了优秀文档以供学习,如果有兴趣可以自行阅读 。如果大家有什么疑问欢迎在评论区留言 。
 
参考资料https://www.studytonight.com/post/check-if-user-is-offline-online-in-javascript
https://daily-dev-tips.com/posts/detecting-if-the-user-is-online-with-javascript/
https://medium.com/@codebubb/how-to-detect-if-a-user-is-online-offline-with-javascript-b508fc595f2
https://stackoverflow.com/questions/3181080/how-to-detect-online-offline-event-cross-browser

封面图:来自iamabhishek的文章,链接为:
https://www.studytonight.com/post/check-if-user-is-offline-online-in-javascript




推荐阅读