我有一个运行WinJS进程线程的Windows Phone Phonegap/Cordova应用程序。但是,这确实需要在应用程序初始化和后台运行 - 间隔轮询。
我基本上想做的是有一个PhoneGap应用程序,该应用程序执行AJAX调用作为后台进程,以便在文件从服务器发送时保持最新状态。
setInterval(function() {
// Check data with $.ajax();
}, 30000);
我一直在用这个撕扯我的头发,我尝试了太多代码,我的大脑被炸了。我确信一定是可能的,但我无法弄清楚如何将 AJAX 调用作为后台服务运行......
先发制人的"为帮助的男孩和女孩加油"!
您需要在
应用程序启动时以及每次计时器用完时更新一些信息。对吗?
我会这样做:
function yourMainFunction(){
try{
if(localStorage){
//A flag that indicates if it is App Instalation (First Use) or Not
if(!localStorage.appRunFirstTime){
callLoadingStructure();
//Assuming that you data file is a TXT file and we have a JSON Structure inside it
setTimeout(function(){
$.ajax({
type: 'GET',
dataType: 'text',
url: 'yourSite/yourData.txt',
success: function (data) {
var content = JSON.parse(data);
//The JSON parsed DATA
doWhateverYouNeedWithTheData(content);//This method should use the data from your server
localStorage.setItem("appRunFirstTime", "OK!!!");
closeLoadingStructure();
}
error: function(errowThrown){
console.log(errowThrown);
}
});
//Minimun timeout to show Loading Structure even if the request works without any lag
}, 1000);
}
//App is already instaled
else{
setInterval(function(){
$.ajax({
type: 'GET',
dataType: 'text',
url: 'yourSite/yourData.txt',
success: function (data) {
var content = JSON.parse(data);
//The JSON parsed DATA
doWhateverYouNeedWithTheData(content);
//updateDOM?(); if you need to update the DOM when new information comes up
}
error: function(errowThrown){
console.log(errowThrown);
}
});
//Minimun timeout to show Loading Structure even if the request works without any lag
}, 30000);
}
}
}
catch(error){
console.log(error);
}
}
function callLoadingStructure(){
//Its a example of a loading structure while the first content loads
document.getElementById("yourMainContentDiv").style.display = "none";
document.getElementById("yourLoadingDiv").style.display = "block";
}
function closeLoadingStructure(){
//Cloasing loading structure and showing maindiv
document.getElementById("yourMainContentDiv").style.display = "block";
document.getElementById("yourLoadingDiv").style.display = "none";
}
希望对您有所帮助。此致敬意!