How to detect the Internet connection is offline in JavaScript?
There are 2 answers forthis for two different senarios:-
If you are using JavaScript on a website(i.e; or any front-end part) The simplest way to do it is:
The Navigator Object
The onLine property returns true if the browser is online:
But if you're using js on server side(i.e; node etc.), You can determine that the connection is lost by making failed XHR requests.
The standard approach is to retry the request a few times. If it doesn't go through, alert the user to check the connection, and fail gracefully.