Alert box when no internet connection - Phonegap

后端 未结 9 2821
南方客
南方客 2021-02-20 06:55

I\'m trying to get a pop-up to, well, pop up when there is no internet connection on the device.

I got the following example working, but now I want the alert only to sh

9条回答
  •  甜味超标
    2021-02-20 07:42

    I'm just amalgamating a couple of answers here, but I wanted something which responded to the online/offline events but also knew at the start whether there was an internet connection. I was using this in an Angular controller which broadcast about state changes, but I've removed those parts for simplicity.

    document.addEventListener("deviceready", onDeviceReady, false);
    
    function onDeviceReady() {
        var _curStatus = false;
    
        function toggleStatus(newVal) {
            console.log('Setting internet connection state to: ' + newVal);
            _curStatus = newVal;
            // My angular $broadcast went here
        }
    
        var conType = navigator.network.connection.type;
        toggleStatus((conType != Connection.NONE) && (conType != Connection.UNKNOWN));
        document.addEventListener("online", function() {toggleStatus(true);}, false);
        document.addEventListener("offline", function() {toggleStatus(false);}, false);
        }
    }
    

提交回复
热议问题