Online and offline event are firing 2 times in a row

hi there ,
i create a new ionic project simple one only to try the online and offline event ,
ii have tried several ways and i always get the event fires 2 times ,
i have tried these:

.run(function($ionicPlatform ) {
  $ionicPlatform.ready(function() {

           var onOnline=function(){
					
			console.log('online' ');
			
		};
		var onOffline=function(){
			console.log('onOffline');
		};
		 


           document.addEventListener("online", onOnline, false);
           document.addEventListener("offline", onOffline, false);
    

  }
})


.run(function($ionicPlatform ,$rootScope ,$cordovaNetwork ) {
  $ionicPlatform.ready(function() {

           $rootScope.$on('$cordovaNetwork:online', function(event, networkState){
              console.log('on online', event, networkState);
          
    })

    // listen for Offline event
    $rootScope.$on('$cordovaNetwork:offline', function(event, networkState){
      var offlineState = networkState;
      console.log('on offline',)
      })
    

  }
})

and i have no solution, i always het the online an offline events fire twice in a row

thanks for reading and try helping

.run(function($ionicPlatform, $rootScope, $cordovaNetwork) {
  $ionicPlatform.ready(function() {
    if (window.Connection) {
      $rootScope.$on('$cordovaNetwork:online', function(event, networkState) {
        alert('online good sir');
      })
      $rootScope.$on('$cordovaNetwork:offline', function(event, networkState) {
        alert('offline good sir');
      })
    }
  });
})

Seems to work fine for me.

One thing I notice is in your pure js example, you have an extra '

 var onOnline=function(){
					
			console.log('online' ');

should be

 var onOnline=function(){
					
			console.log('online');
1 Like

thnks for your reply, appreciate your help,
but i cant fixed the problem :
console.log(window.Connection); //i get undefined on debug, why???

if (window.Connection) {
  $rootScope.$on('$cordovaNetwork:online', function(event, networkState) {
    alert('online good sir');
  })
  $rootScope.$on('$cordovaNetwork:offline', function(event, networkState) {
    alert('offline good sir');
  })
}

window.Connection is the short had to check if the method is available.

Same thing as the status bar

if(window.StatusBar) {
  // Set the statusbar to use the default style, tweak this to
  // remove the status bar on iOS or change it to use white instead of dark colors.
  StatusBar.styleDefault();
}

We are just checking if the functionality is there before calling any methods

Does this mean that if the app fires without internet connection the event will not get attached, therefore, will we lose the ability to notify the user if they have lost connection?

it is normal take 3 seconds to fire online/offline events ?

@enddynayn No, it’s actually checking for the objects/functions attached to window.Connection not the actual connection so you can use that online/offline with no problem.

@pjusto In my experience yes

1 Like

@sebastian11c did you get a solution? I’m getting the same thing.

Using code that @mhartington posted but the events fire twice every time.

Is there a possibility that this can be device related Issue?

1 Like

hi @DickSwart, i think is a device bug, i tested the code in 3 different devices ang only had issues on LG optimus G.
regards

I have the same problem @DickSwart

Is it bad practice for any reason to instead check the connection in the .config and possibly change the default route based on that outcome?

This is exactly what I had experienced a while ago.

Here is a link that explains 2 easy solutions for the problem.
http://www.yourtechchick.com/cordovanetwork-online-and-offline-events-fired-twice/.

Hope that helps.