我正在使用离线js进行互联网上下检测。
我的代码如下
$(document).ready(function () {
Offline.options = {
reconnect: {
initialDelay: 10,
delay: (5)
}
}
Offline.on('up', internetUp);
Offline.on('down', internetDown);
function internetUp(event, data) {
console.log("Internet is up.");
}
function internetDown(event, data) {
console.log("Internet is down.");
}
});
我面临的问题是,当Internet断开时,没有事件触发,而当Internet断开时,两个事件都触发了,我需要使用离线JS 0.7.13版本的帮助。
最佳答案
检查它是否可以在没有第三方模块的情况下使用。
您可以检查浏览器的navigator.onLine
值
https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine
在上面的代码中,在您的ready函数中,您可以将eventHandler附加到检测在线/离线事件的窗口中
window.addEventListener('online', updateOnlineStatus);
window.addEventListener('offline', updateOnlineStatus);
https://developer.mozilla.org/en-US/docs/Online_and_offline_events