Javascript 电话线
我正在使用以下脚本检查设备是联机还是脱机:Javascript 电话线,javascript,jquery,cordova,jquery-mobile,Javascript,Jquery,Cordova,Jquery Mobile,我正在使用以下脚本检查设备是联机还是脱机: function checkConnection() { document.addEventListener("online", onDeviceOnline, false); document.addEventListener("offline",onDeviceOffline, false); function onDeviceOnline(){ loadZive(); loadMobil()
function checkConnection() {
document.addEventListener("online", onDeviceOnline, false);
document.addEventListener("offline",onDeviceOffline, false);
function onDeviceOnline(){
loadZive();
loadMobil();
loadAuto();
};
function onDeviceOffline(){
alert("deviceIsOffline");
};
};
checkConnection();
然后我有这个函数来加载提要:
function loadZive(publishedDateConverted){
google.load("feeds", "1");
function initialize() {
var feed = new google.feeds.Feed("http://www.zive.sk/rss/sc-47/default.aspx");
feed.setNumEntries(window.localStorage.getItem("entriesNumber"));
feed.load(function(result) {
if (!result.error) {
var feedlist = document.getElementById("feedZive");
for (var i = 0; i < result.feed.entries.length; i++) {
var li = document.createElement("li");
var entry = result.feed.entries[i];
var A = document.createElement("A");
var descriptionSettings = window.localStorage.getItem("descriptionSettings");
if (descriptionSettings=="true"){
var h3 = document.createElement("h3");
var p = document.createElement("p");
var pDate = document.createElement("p");
pDate.setAttribute("style","text-align: right; margin-top: 5px;");
var publishedDate = new Date(entry.publishedDate);
publishedDateConverted = convertTime(publishedDate);
pDate.appendChild(document.createTextNode(publishedDateConverted));
h3.setAttribute("style","white-space: normal;")
h3.appendChild(document.createTextNode(entry.title));
p.setAttribute("style","white-space: normal;")
p.appendChild(document.createTextNode(entry.content));
A.setAttribute("href",entry.link);
A.appendChild(h3);
A.appendChild(p);
A.appendChild(pDate);
}
else{
A.setAttribute("href",entry.link);
A.setAttribute("style","white-space: normal;")
A.appendChild(document.createTextNode(entry.title));
};
li.appendChild(A);
feedlist.appendChild(li);
}
$("#feedZive").listview("refresh");
}
});
}
google.setOnLoadCallback(initialize);
};
然后一切正常,所以我认为这与在线和离线事件监听器有关。当我将checkconnection放入onDeviceReady函数时,它也不起作用,所以这应该不是问题所在。所以,有没有办法检查设备是否在线,然后使用js文件加载提要
编辑:我使用Simon McDonald建议并创建了如下代码:
函数ondevicerady(){
使用此代码,警报在设备联机和设备脱机时都能正常工作,但当我尝试加载提要时,得到的结果与以前相同(页面布局加载,然后所有内容都变为空白页面)。问题是您添加了“联机”设备就绪事件监听器中的事件监听器,但设备已联机,因此在连接发生更改之前,事件不会再次触发。在设备就绪事件监听器中,您应该检查navigator.connection.network.type的值,并确保该值不是NONE
谢谢,我会尝试一下,但到目前为止它看起来不错。我不知道phonegap中有类似navigator.network.connection.type;的内容,所以我没有使用它。我使用了您的建议来创建代码(如编辑中所示)但我还是得到了和以前一样的结果。我不知道我是否能实现这个功能,因为我尝试的一切都失败了,我只得到了上面描述的空白屏幕
function loadFeeds(){
loadZive();
loadMobil();
loadAuto();
};
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown(){
navigator.app.exitApp();
}
function checkConnection() {
var networkState = navigator.network.connection.type;
if (networkState == "none"){
alert("no network connection");
}
else{
loadZive();
loadMobil();
loadAuto();
};
};
checkConnection();
};