0

我正在使用下面的这个函数来获取互联网连接类型

function checkConnection() {
        var networkState = navigator.network.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.NONE]     = 'No network connection';

        alert('Connection type: ' + states[networkState]);


}

这会提醒状态: alert('Connection type: ' + states[networkState]);

我需要做的是只有在状态为 states[Connection.NONE] 时才会发出警报

我试过:

if ((states[networkState]) = states[Connection.NONE]) {
    alert('No internet connection here');
}

但这没有用。

4

2 回答 2

1

从逻辑上讲,这

if ((states[networkState]) = states[Connection.NONE])

应该

if ((states[networkState]) == states[Connection.NONE])
于 2012-09-11T08:30:09.840 回答
1

更好的方法是从 if 语句中删除该函数。

if (networkState == Connection.NONE) {
    alert('No internet connection here');
}
于 2012-09-11T08:31:29.780 回答