【问题标题】:Phonegap Infinite Loop Network Connection CheckPhonegap 无限循环网络连接检查
【发布时间】:2014-02-27 19:42:48
【问题描述】:

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

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

有什么建议吗?

非常感谢

【问题讨论】:

  • 它本来是无限运行的,但感谢 Nijil

标签: android cordova infinite-loop network-connection


【解决方案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);

【讨论】:

  • 谢谢 Sam :) 我添加了事件监听器并删除了循环
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多