每分钟使用jquery移动检查设备互联网连接
Check device internet connection every minute with jquery mobile
每
创建一分钟页面都会检查互联网连接。但是,当设备上的wifi关闭时,页面将被重定向到no_internet.hmtl页面。 我们如何在每次打开/关闭wifi按钮时检查互联网连接,页面应重定向到no_internet.html
$(document).on('pagebeforecreate', '[data-role="page"]', function(){ //loading spinner
setInterval(
function(){
if(window.navigator.onLine){
}else {
window.location='./no_internet.html';
return false;
}
}, 1000);
setTimeout(function(){
$.mobile.loading('show', {
text: 'Chargement en cours...',
textVisible: true,
theme: 'a',
html: "<span class='ui-bar ui-overlay-c ui-corner-all' ><img width='50px' height='50px' src='http://www.shougun.it/images/loading.gif' /><br><h2>Chargement en cours...</h2></span>"
});
},5);
});
var interval=setInterval(function(){
connectionExist(); // function returns a true if an internet connection exists
}, 1000);
function connectionExist() {
var xhr = new XMLHttpRequest();
var file = "http://localhost:50041/stackflow/icon1.png"; //URL we specify is the path to the file that we want to check on
var randomNum = Math.round(Math.random() * 10000);
xhr.open('HEAD', file + "?rand=" + randomNum, false);
try {
xhr.send();
if (xhr.status >= 200 && xhr.status < 304) {
console.log('Connected');
return true;
} else {
console.log('Connection Exist');
clearInterval(interval);
window.location.href = "http://localhost:50041/stackflow/noInternet.html";
return false;
}
} catch (e) {
return false;
}
}
相关文章:
- 当没有互联网连接时,谷歌地图Java脚本API处理
- 如果没有互联网连接,Android WebView离线消息
- 捕获”;没有互联网连接”;AngularJS中
- 我如何通过从服务器调用gif来检查是否有互联网连接
- 如果没有互联网连接,请隐藏iframe
- 如何捕捉不稳定的互联网连接
- 使用jquery检查互联网连接
- 使用 html5 的互联网连接代码
- 如果没有互联网连接,我想显示吐司,不知道该怎么做
- Jquery 移动错误消息,当没有互联网连接加载谷歌地图
- Jquery移动检查时没有互联网连接显示错误消息,而不是加载谷歌地图
- 当互联网连接丢失时,如何从 Ajax 调用中获取信息
- Angular js,如何在调用url之前检查用户是否有互联网连接
- 互联网连接检查不起作用
- 基于英特尔 xdk html5 的应用程序只能在启用互联网连接时工作
- 在javascript中检查互联网连接的最佳实践
- 如何检查移动互联网连接
- 使用 JavaScript 检测互联网连接
- 在JS中检查互联网连接的最快方法
- 在PhoneGap应用程序中检测缓慢的互联网连接