在设备上的PhoneGap检查Internet连接VS手机浏览器 [英] PhoneGap check Internet connection on device vs mobile browser

查看:197
本文介绍了在设备上的PhoneGap检查Internet连接VS手机浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的PhoneGap和JqueryMobile。我需要检查设备的网络连接。看到网络上的一些解决方案之后我感到困惑的方法。一些解决方案是在暗示一些使用JQMobile方法来检查连接,同时使用PG API如网络管理器和连接。

I am new to Phonegap and JqueryMobile. I need to check the internet connection on device. After seeing some solution on net I got confused about approaches. some solutions is suggesting to use PG API like network manager and connection while some using JQMobile method to check connection.

我的问题是其中preFER?还是什么都为实施目标区?

My question is which to prefer ? or what are the target areas for both the implementation ?

推荐答案

试试这个

工作实例 http://jsfiddle.net/Gajotres/d5XYR/

在这种情况下,计时器会检查互联网连接每隔100毫秒和最终的结果集为JavaScript全局变量。

In this case timer will check internet connection every 100 ms and set final result into a javascript global variable.

一切都取决于这一行:

window.navigator.onLine -- it will be false if the user is offline.

最终的解决方案:

Final solution:

var connectionStatus = false;

$(document).on('pagebeforeshow', '#index', function () {
    setInterval(function () {
        connectionStatus = navigator.onLine ? 'online' : 'offline';
    }, 100);
    $(document).on('click', '#check-connection', function () {
        alert(connectionStatus);
    });
});

这篇关于在设备上的PhoneGap检查Internet连接VS手机浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆