我有一个运行WinJS流程线程的Windows Phone Phonegap / Cordova应用程序。但是,这确实需要在应用程序初始化和后台运行 - 在一个时间间隔内进行轮询。
我基本上想要做的是拥有一个PhoneGap应用程序,该应用程序执行AJAX调用作为后台进程,以便在从服务器发送文件时保持文件最新。
$scope.test = function(){
$timeout(function () {
document.getElementById('test').select();
}, 0);
};
我一直在用这个撕开我的头发,我已经尝试了很多代码,我的脑子被炒了。我敢肯定它一定是可能的,但我无法弄清楚如何将AJAX调用作为后台服务...
先发制人的“为帮助男女们欢呼”!
答案 0 :(得分:1)
您需要在App启动时以及每次计时器用完时更新一些信息。是不是?
我会这样做:
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";
}
希望它有所帮助。最好的问候!