现在我正在使用此代码检查互联网连接。它只检查触发或调用的时间:
function Animal (name) {
this.name = name;
}
Animal.prototype.speak = function () {
console.log(this.name + ' makes a noise.');
}
我想要的是每当互联网消失它应该提醒(不使用.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';
}
}
}
};
})
因为它在后台运行时降低了应用程序的性能)
还有其他办法吗?
答案 0 :(得分:1)
请查看我的其他帖子:Trying to rewrite controller as service, so that it can check network status all over app。
您只需添加以下内容即可随时随地查看您的互联网连接:
if(ConnectivityMonitor.isOnline()){
//do something
}else{
//do something else
alert("No internet connection");
}