The TaskRunner class is used to execute a function at a specified interval. This is useful when doing polling type operations, for example when reloading Ajax content every 30 seconds. The TaskMgr object is a singleton instance of TaskRunner, it can be used for quick access to a TaskRunner.
var stop = false;
var task = {
run: function(){
if(!stop){
alert(new Date());
}else{
runner.stop(task); // we can stop the task here if we need to.
}
},
interval: 30000 // every 30 seconds
};
var runner = new Ext.util.TaskRunner();
runner.start(task);
// Using TaskMgr
Ext.TaskMgr.start({
run: function(){
},
interval: 1000
});
The DelayedTask class provides a convenient way to "buffer" the execution of a method. When called, the task will wait the specified time period before executing. If durng that time period, the task is called again, the original call will be cancelled. This continues so that the function is only called a single time for each iteration. This method is especially useful for things like detecting whether a user has finished typing in a text field.
var task = new Ext.util.DelayedTask(function(){
alert(Ext.getDom('myInputField').value.length);
});
// Wait 500ms before calling our function. If the user presses another key
// during that 500ms, it will be cancelled and we'll wait another 500ms.
Ext.get('myInputField').on('keypress', function(){
task.delay(500);
});
Note that we are using a DelayedTask here to illustrate a point. The configuration option buffer for addListener/on will also setup a delayedtask for you to buffer events.