如何使用javascript或任何库检查用户是否在线? [英] How to check if the user is online using javascript or any library?

查看:113
本文介绍了如何使用javascript或任何库检查用户是否在线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果可以使用 Javascript jQuery 或任何库(如果有)检查互联网连接,我需要一些帮助。因为我正在开发一个离线应用程序,我希望显示一个版本,如果用户离线,另一个版本,如果用户在线。



目前我是使用此代码:



  if(navigator.onLine){alert('online');} else {alert('offline');}  



但检测速度非常慢。有时它只是连接到没有互联网的网络,需要5到10秒才能提醒 false (没有互联网)。



<我看了一下Offline.js库,但我不确定这个库在我的情况下是否有用。我不知道怎么用它

解决方案

我想你应该试试 OFFLINE.js ..它看起来很容易使用,只需试一试。



它甚至提供选项 checkOnLoad ,它会在页面加载时立即检查连接。


Offline.check():检查连接的当前状态。



Offline.state:连接的当前状态'up'或'down'


还没试过,会很好知道它是否按预期工作。



EDIT 在代码中占了一席之地,它使用失败的XHR请求方法建议在这个 SO问题


I need some help on how I could check the internet connection using Javascript or jQuery or any library if available. cause i'm developing an offline application and I want to show a version if the user is offline and another version if the user is online.

For the moment i'm using this code :

if (navigator.onLine) {
    alert('online');
} else {
    alert('offline');
}

But this is working very slow to detect. sometimes it's just connected to a network without internet, it takes 5 to 10 seconds to alert false (No internet).

I took a look at Offline.js library, but I'm not sure if this library is useful in my case. and I don't know how to use it

解决方案

i think you should try OFFLINE.js.. it looks pretty easy to use, just give it a try.

it even provides the option checkOnLoad which checks the connection immediately on page load.

Offline.check(): Check the current status of the connection.

Offline.state: The current state of the connection 'up' or 'down'

haven't tried it, would be nice to know if it works as intended.

EDIT took a little peak into the code, it uses the method with FAILED XHR REQUEST suggested in THIS SO Question

这篇关于如何使用javascript或任何库检查用户是否在线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆