Jquery - 检查Internet连接

时间:2017-07-17 13:32:39

标签: javascript jquery google-chrome internet-connection

我想创建jquery监听器来检查互联网连接。



    function checkInternetConnection()
    {
        onConnectionClosed:function(){
            dialog.show("Connection closed please wait");
        }
        onConnectionOpened:function(){
            dialog.hide();
        }
    }
    
    $(document).ready(function(){
        checkInternetConnection();
    });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

因此,当关闭连接时,我想向用户显示一个对话框。当连接回来时我想隐藏对话框。我怎么能这样做?

1 个答案:

答案 0 :(得分:5)

这是来自MDN的片段:

https://developer.mozilla.org/en-US/docs/Online_and_offline_events

window.addEventListener('load', function() {

  function updateOnlineStatus(event) {
    var condition = navigator.onLine ? "online" : "offline";

    alert(`you are ${condition}`)

  }

  window.addEventListener('online',  updateOnlineStatus);
  window.addEventListener('offline', updateOnlineStatus);
});