How to alert the user when there's no internet connection

前端 未结 5 1086
深忆病人
深忆病人 2020-12-30 14:46

I need to alert the user with the following conditions;

  1. Request timed out
  2. No internet connection
  3. Unable to reach the server

He

5条回答
  •  北海茫月
    2020-12-30 15:07

    Exactly as Simon said, you can use

    document.addEventListener("offline", youCallbackFn, false);
    

    or you can interrogate the boolean property

    navigator.onLine
    

    (Should return true or false)

    However, this technique will tell you whether device is connected. The caveat is such that device can be connected to WiFi, but the router might be offline. In that case, use a polling mechanism, like timely Ext.Ajax.request with lower timeouts. Timeout expired = offline.

提交回复
热议问题