简体   繁体   English

什么是在Ionic中检查互联网连接的最佳方式

[英]What is the Best way to check internet connection in Ionic

Right Now I am Using this code to check the internet connection. 我现在正在使用此代码检查互联网连接。 And it only checks when it is triggered or called: 它只检查触发或调用的时间:

.factory('Checkinternet', function($ionicPlatform,$ionicPopup) { 
  return {
      isonline: function() {
         if(window.Connection) {
              if(navigator.connection.type == Connection.NONE){
                    $ionicPopup.confirm({
                      title: 'No Internet Connection',
                      content: '<span style="color:black">Sorry, no Internet connectivity detected. Please reconnect and try again.</span>'
                    }).then(function(result) {
                      if(!result) {
                        //ionic.Platform.exitApp();
                      }
                    });
                    return 'off';
              }
          }
      }
  };
})

What I want is whenever Internet goes away It should Alert (Without Using Setinterval as it is slowing the performance of my app when it runs in background) 我想要的是每当互联网消失它应该警告(不使用Setinterval因为它在后台运行时减慢我的应用程序的性能)

Is there any other way Of doing this ?? 有没有其他方法这样做?

Please check my other post: Trying to rewrite controller as service, so that it can check network status all over app . 请检查我的其他帖子: 尝试将控制器重写为服务,以便它可以检查整个应用程序的网络状态

You can check your internet connection anywhere you want by simply add: 您只需添加以下内容即可随时随地查看您的互联网连接:

if(ConnectivityMonitor.isOnline()){

   //do something

}else{

  //do something else 
  alert("No internet connection");
}   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM