将后台进程添加到我的 Windows Phone 应用

Adding a Background Process to my Windows Phone app

我有一个 Windows Phone Phonegap/Cordova 应用程序 运行 正在使用 WinJS 进程线程。但是,这确实需要 运行 在应用程序初始化和后台 - 按时间间隔轮询。

我基本上想要做的是有一个 PhoneGap 应用程序,它执行一个 AJAX 调用作为后台进程,以在文件从服务器发送时保持最新。

setInterval(function() {
    // Check data with $.ajax();
}, 30000);

我一直在用这个把我的头发扯掉,我已经尝试了很多代码,我的大脑被炸了。我确定这一定是可能的,但我不知道如何 运行 一个 AJAX 调用作为后台服务...

先发制人"cheers for the help guys and girls"!

您需要在应用程序启动时和每次计时器用完时更新一些信息。对吗?

我会这样做:

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";
}

希望对您有所帮助。此致!