gpt4 book ai didi

android - Phonegap 无限循环网络连接检查

转载 作者:行者123 更新时间:2023-11-30 03:07:34 25 4
gpt4 key购买 nike

请帮忙。我有以下无限运行的功能:

    // 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。

有什么建议吗?

非常感谢

最佳答案

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);

关于android - Phonegap 无限循环网络连接检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21542306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com