Reputation: 10713
I have a Windows Phone Phonegap/Cordova App that is running a WinJS process thread. However, this really needs to run on app initialisation and in the background - polling on an interval.
What I basically want to do is have a PhoneGap app which executes an AJAX call as a background process to keep files up to date as they are sent from a server.
setInterval(function() {
// Check data with $.ajax();
}, 30000);
I have been tearing my hair out with this one, I've tried so much code my brain is fried. I'm sure it must be possible, but I cannot figure out how to run an AJAX call as a Background Service...
Pre-emptive "cheers for the help guys and girls"!
Upvotes: 0
Views: 87
Reputation: 301
You need to update some information when the App starts and everytime a timer runs out. Is it right?
I would do this way:
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";
}
Hope it helps. Best regards!
Upvotes: 1