Ionic check internet connection every seconds?


#1

Ionic check internet connection enabled or not in every 2 seconds. when open my app


#2

Bad idea, IMHO. Deal with it when you are needing a network connection, and not at an arbitrary interval.


#3

my idea is wrong fine. but when i submit my data that time i will off my network. that time give a alert for network is off. that is my question.


#4

I don’t understand what you are writing, but I see no benefit in bothering the user about network status at any time other than when a network request fails.


#5

I usually use this

this.network.onChange().subscribe(() => {
  switch (this.network.type) {
    case '2g':
      console.log('probably not very fast ...');
      break;
    case 'wifi':
      console.log('wohoo wifi ...');
      break;
  }
});

#6

my question is check my internet status every 2 seconds


#7

I am trying my best to be polite, but my answer is that I think you should not be doing that.


#8

Ok thanks. thanks for your clarifications


#9

And if you need always a connection? It’s a bad idea do a subscribe?


#10

i got my issues in my code.


#11

You can check this thread too


#12

hello sir,
how i can check no internet access when wifi/mobile data connected in ionic 3.
my code is:
//---- check internet -----

	this.alert = this.alertCtrl.create({
		title: 'Disconnected',
		message: 'Please connect your device to internet',
		buttons: [
		{
			text: 'Close',
			handler: () => {
			   this.platform.exitApp();
			}
		}
		],  enableBackdropDismiss: false 
	});	  
  	
	this.network.onDisconnect().subscribe(res=>{
	  this.online=false;
	  this.Status='Disconnected';
	  console.log('network was Disconnected');
      this.alert.present();			
	});

	this.network.onConnect().subscribe(res=>{				

		if(this.network.type == "unknown" || this.network.type == "none" || this.network.type == undefined){
			alert("The device is not online"+this.network.type);
			//console.log("The device is not online");
			this.online = false;
			this.Status='Disconnected';
			this.alert.present();	
		}else{
			alert("The device is online"+this.network.type);
			this.online = true;
			this.Status='Connected';
			this.alert.dismiss();
		}
  			
		//define again alert
		this.alert = this.alertCtrl.create({
		title: 'Disconnected',
		message: 'Please connect your device to internet',
		buttons: [
		{
			text: 'Close',
			handler: () => {
			   this.platform.exitApp();
			}
		}
		],  enableBackdropDismiss: false 
		});
	});

#13

this.task = setInterval(() => {
this.checkInternet(); //function there you can do your code for Network inplementation
}, 3000);