我想创建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;
因此,当关闭连接时,我想向用户显示一个对话框。当连接回来时我想隐藏对话框。我怎么能这样做?
答案 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);
});