如何检查设备是在线还是离线Phonegap [英] How to check device is Online or Offline Phonegap

查看:186
本文介绍了如何检查设备是在线还是离线Phonegap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查设备(智能手机)是否在线或离线是最简单的方法。
我使用phonegap,jquery mobile。
我找到了这个。

What is the easiest way to check whether the device(smartphone) is online or offline. I'm working with phonegap, jquery mobile. I found this one.

document.addEventListener("online", yourCallbackFunction, false);

我想做的是检查互联网连接并决定从互联网获取数据或从设备本地数据库。

What I want to do is to check the Internet connection and decide about to get the data from the internet or from the device local database.

if(deviceoffline)
  getDataFromLokalDatabase();
else
  getDataFromInternet();


推荐答案

您可以使用 navigator.onLine

You can use navigator.onLine:

var isOffline = 'onLine' in navigator && !navigator.onLine;

if ( isOffline ) {
    //local db
}
else {
    // internet data
}

但请注意,如果 navigator.onLine isOffline 将始终为false

But be aware of that if navigator.onLine isn't supported isOffline will always be false

这篇关于如何检查设备是在线还是离线Phonegap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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