문제

I have to check for network connection on app start, but PhoneGap online/offline event fires after any state change to it. If the app starts in online mode, the online events doesn't fired, until it gets offline, then comes online.

This is the code,

initialize: function() {
    this.bindEvents();
},

bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},

onDeviceReady: function() {        
    document.addEventListener("offline", this.onOffline, false);
    document.addEventListener("online", this.onOnline, false);
},

That's wrong with the code ?

도움이 되었습니까?

해결책

The online/offline events are made to fire on state change only. If you need to check the connectivity at a certain point, you can use navigator.network.connection.type.

initialize: function() {
    this.bindEvents();
},

bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},

onDeviceReady: function() {        
    document.addEventListener("offline", this.onOffline, false);
    document.addEventListener("online", this.onOnline, false);
    if((navigator.network.connection.type).toUpperCase() == "NONE" && 
       (navigator.network.connection.type).toUpperCase() == "UNKNOWN") {
        this.onOffline();
    }
    else {
        this.onOnline();
    }   
},

Hope this helps you.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top