PDA

View Full Version : Stopping a periodical task after a certain event



KajaSheen
26 Jun 2013, 2:10 AM
I am trying to monitor changes to a record on the remote backend. I save a task with a certain status attribute (i.e. "start") and now I want to periodically poll the backend if the status has been changed, because some background processing has been done.

My guess is that I start a TaskManager Task to check for changes to the record, but how do I stop the task once the change has been detected, because at that point I do not have a reference to the task anymore? Should I put a reference into the record or something?

Farish
26 Jun 2013, 2:29 AM
I havent used TaskManager but I can suggest something else. you can use the standard setTimeout or setInterval functions of Javascript and store the reference in a variable. then you can use clearTimeout to stop the polling.

See the following example. setTimeout executes only once and you have to call it again if you want to perform some task repeatedly OR you can use setInterval:


var timerVar = setTimeout("myFunction()", 5000);

function myFunction()
{
if(condition == true) // check your condition to see if you want to repeat the function call
timerVar = setTimeout("myFunction()", 5000);
else // stop the repeating
clearTimeout(timerVar);
}

slemmon
27 Jun 2013, 2:20 PM
In the even you go the TaskRunner route the following example might help give some direction:


var updateClock = function () {
console.log(Ext.Date.format(new Date(), 'g:i:s A'));
}


var runner = new Ext.util.TaskRunner();
var task = runner.newTask({
run: updateClock,
interval: 1000
});
runner.start(task);


Ext.widget('button', {
renderTo: document.body,
text: 'Pause Task',
enableToggle: true,
toggleHandler: function (btn, pressed) {
pressed ? runner.stop(task) : runner.start(task);
}
});