请帮忙。我有以下无限运行的功能:
// Check the connection state of the device
function checkConnection() {
var networkState = "";
networkState = navigator.connection.type;
var states = {};
states[Connection.UNKNOWN] = 'Unknown connection';
states[Connection.ETHERNET] = 'Ethernet connection';
states[Connection.WIFI] = 'WiFi connection';
states[Connection.CELL_2G] = 'Cell 2G connection';
states[Connection.CELL_3G] = 'Cell 3G connection';
states[Connection.CELL_4G] = 'Cell 4G connection';
states[Connection.CELL] = 'Cell generic connection';
states[Connection.NONE] = 'No network connection';
if (states[networkState] === 'No network connection') {
noNetworkAlertCreate();
} else {
noNetworkAlertRemove();
}
checkConnection();
}
设备准备就绪后,会调用此功能:
// Device is ready let's do this
function onDeviceReady() {
checkConnection();
}
该函数已正确循环,但连接状态在某种程度上不会随每个循环更新。如果状态首次注册为WIFI,则在再次运行时不会更新,并且即使已禁用WIFI,也会再次注册为WIFI。
有什么建议吗?
非常感谢
答案 0 :(得分:1)
ondeviceready只会在cordova满载时触发一次。当“在线”和“离线”等不同事件发生时,您需要调用相同的功能。:
// Device is ready let's do this
function onDeviceReady() {
checkConnection();
}
document.addEventListener("deviceready", onDeviceReady, false);
document.addEventListener("online", checkConnection, false);
document.addEventListener("offline", checkConnection, false);