13

Check the status of network connection in web apps

 3 years ago
source link: https://jsmates.com/blog/listen-to-network-connection-in-javascript
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Check the status of network connection in web apps

Friday, December 25th 2020 (5 days ago)
Param Harrison@learnwithparam

You can listen to network status changes through event listeners.

// listening whether the network comes online
window.addEventListener('online', event => {
  console.log('Your network is online again');
});

// listening whether the network goes offline
window.addEventListener('offline', event => {
  console.log('You network connection is lost');
});

You can also check the current status of the network,

// Check the current status of the network
console.log(window.navigator.onLine); // true - Online, false - offline

Often, apps show a toast message that connection is lost. This is how they achieve it and notify the user real-time on the page.

Toggle your network and check the console to test it out,


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK