setInterval does not work on background ?

You must Login before you can answer or comment on any questions.

When I used setInterval() with a simple function, it works ok, even my app runs in background

var timer;
 
var countTimer = function (caller) {
    if (Titanium.Network.online) {
        times++;
        myLabel.text = 'Interval : ' + times + ' times';
        //Titanium.API.info(' run timer : ' + times + ' by Trang');
    }
 
    if (times == 50) {
        clearInterval(timer);
    }
};
var timer = setInterval(countTimer, 1000);
But when I try to get data from remote site, it does not work when my app runs in background
var timer;
 
var countTimer = function (caller) {
    if (Titanium.Network.online) {
       GetData(url);
    }
};
var timer = setInterval(countTimer, 300000);
Please help me !!!

2 Answers

I would recommend to forget such a Solution. Why you want update anything although the user cannot see it? This just consumes battery power and traffic. Instead use 'resume' event to update the data and then you can start your timer for frequently updating. Also be aware: background Services are limited to a duration of 10 mibutes.

Your Answer

Think you can help? Login to answer this question!