Offline.js - online check?

Here is my code below.

I just don’t know how to do this if the connection goes from ONLINE to OFFLINE, which is an event that updates css ( https://github.com/hubspot/offline ).

If it switches from OFFLINE to ONLINE, it is updated within 10 seconds. But I'm not sure how this works in another direction?

It seems if I call Offline.check (); manually it will be updated, but ideally I would like it to be done automatically. Any ideas are welcome.

Offline.options = {
    checkOnLoad: true,
    checks: {
        image: {
            url: 'http://example.com/image.gif'
        },
        active: 'image'
    },
    reconnect: {
        initialDelay: 10, // only check every 10 seconds
        delay: 10
    }
};
+1
source share
3 answers

You can use Offline.on(event, handler, context)to find out when a connection is connected to a network / offline.

Offline.on('up', function gotOnline(){
  console.log("I am online");
})

Offline.on('down', function gotOffline(){
  console.log("I am offline");
})
+1

, , , Offline.check(), Offline.state.

, CSS, .

:

if(Offline.state=='down'){
    /*assuming that you have that div in your css set to hidden or something else*/
    $("#offlineDiv").show('100'); //Just an imaginary div
}

, .

offline.on(down, handler)..

CSS- div.

+1

Here is my code using the ternary operator

connectionStatus = navigator.onLine ? 'online' : 'offline';
console.log(connectionStatus);
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
Run codeHide result
0
source

Source: https://habr.com/ru/post/1585065/


All Articles