February 2018
Intermediate to advanced
298 pages
8h 22m
English
The navigator.onLine API tells you if the user is online or not. Imagine building a multiplayer game and you want the game to automatically pause if the user loses their internet connection. This is the way to go here!
function state(e) { if(navigator.onLine) { console.log('Cool we\'re up'); } else { console.log('Uh! we\'re down!'); }}window.addEventListener('offline', state);window.addEventListener('online', state);
Here, we're attaching two event listeners to window global. We want to call the state function whenever the user goes offline or online.
The browser will call the state function every time the user goes offline or online. We can access it if the user is offline or online with ...
Read now
Unlock full access