Detect the Internet connection is offline?

后端 未结 19 1410
甜味超标
甜味超标 2020-11-22 00:53

How to detect the Internet connection is offline in JavaScript?

19条回答
  •  伪装坚强ぢ
    2020-11-22 01:46

    There are 2 answers forthis for two different senarios:-

    1. 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:

    2. 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.

提交回复
热议问题