Detect offline/Online status with Javascript/jquery

Source: Internet
Author: User

These two years of offline browsing technology is becoming more and more popular, the most common is the HTML5 mobile application, there are many common web apps also use these technologies. However, the advent of new technologies can sometimes lead to additional distress for our web developers, such as how to tell if a user is now online or offline? Fortunately, there are spears and shields, and the Navigator object in JavaScript helps us solve this problem.

The Navigator.online property provides us with a Boolean value to determine whether the user is connected to the Internet. You can access it like this:

if (navigator.online) {//True|false
// ...
}
No, it's easier than that!

Event

In addition to the ability to detect this offline/online attribute value, we can also bind the offline and AOL events:

function Updateindicator () {
This can be based on the color of the Offline/online button
}
Update online status based on network connectivity
Window.addeventlistener (' online ', updateindicator);
Window.addeventlistener (' Offline ', updateindicator);
Updateindicator ();

Jquery+settimeout can also be used to detect the time between offline/online
var getonline = function () {
if (navigator.online) {
Online time processing
}else{
Updateindicator ();
}
Window.settimeout (Getonline, 1000); Core statements are detected at intervals of 1 seconds
};
jquery calls
$ (document). Ready (function () {getonline ();});

Detect offline/Online status with Javascript/jquery

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.