3 * Copyright(c) 2006-2010 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.util.TaskRunner
9 * Provides the ability to execute one or more arbitrary tasks in a multithreaded
10 * manner. Generally, you can use the singleton {@link Ext.TaskMgr} instead, but
11 * if needed, you can create separate instances of TaskRunner. Any number of
12 * separate tasks can be started at any time and will run independently of each
13 * other. Example usage:
15 // Start a simple clock task that updates a div once per second
16 var updateClock = function(){
17 Ext.fly('clock').update(new Date().format('g:i:s A'));
21 interval: 1000 //1 second
23 var runner = new Ext.util.TaskRunner();
26 // equivalent using TaskMgr
33 * Also see {@link Ext.util.DelayedTask}.
36 * @param {Number} interval (optional) The minimum precision in milliseconds supported by this TaskRunner instance
39 Ext.util.TaskRunner = function(interval){
40 interval = interval || 10;
47 stopThread = function(){
54 startThread = function(){
57 id = setInterval(runTasks, interval);
62 removeTask = function(t){
65 t.onStop.apply(t.scope || t);
70 runTasks = function(){
71 var rqLen = removeQueue.length,
72 now = new Date().getTime();
75 for(var i = 0; i < rqLen; i++){
76 tasks.remove(removeQueue[i]);
84 for(var i = 0, t, itime, rt, len = tasks.length; i < len; ++i){
86 itime = now - t.taskRunTime;
87 if(t.interval <= itime){
88 rt = t.run.apply(t.scope || t, t.args || [++t.taskRunCount]);
90 if(rt === false || t.taskRunCount === t.repeat){
95 if(t.duration && t.duration <= (now - t.taskStartTime)){
104 * @param {Object} task A config object that supports the following properties:<ul>
105 * <li><code>run</code> : Function<div class="sub-desc">The function to execute each time the task is run. The
106 * function will be called at each interval and passed the <code>args</code> argument if specified. If a
107 * particular scope is required, be sure to specify it using the <code>scope</code> argument.</div></li>
108 * <li><code>interval</code> : Number<div class="sub-desc">The frequency in milliseconds with which the task
109 * should be executed.</div></li>
110 * <li><code>args</code> : Array<div class="sub-desc">(optional) An array of arguments to be passed to the function
111 * specified by <code>run</code>.</div></li>
112 * <li><code>scope</code> : Object<div class="sub-desc">(optional) The scope (<tt>this</tt> reference) in which to execute the
113 * <code>run</code> function. Defaults to the task config object.</div></li>
114 * <li><code>duration</code> : Number<div class="sub-desc">(optional) The length of time in milliseconds to execute
115 * the task before stopping automatically (defaults to indefinite).</div></li>
116 * <li><code>repeat</code> : Number<div class="sub-desc">(optional) The number of times to execute the task before
117 * stopping automatically (defaults to indefinite).</div></li>
119 * @return {Object} The task
121 this.start = function(task){
123 task.taskStartTime = new Date().getTime();
124 task.taskRunTime = 0;
125 task.taskRunCount = 0;
131 * Stops an existing running task.
133 * @param {Object} task The task to stop
134 * @return {Object} The task
136 this.stop = function(task){
142 * Stops all tasks that are currently running.
145 this.stopAll = function(){
147 for(var i = 0, len = tasks.length; i < len; i++){
159 * @extends Ext.util.TaskRunner
160 * A static {@link Ext.util.TaskRunner} instance that can be used to start and stop arbitrary tasks. See
161 * {@link Ext.util.TaskRunner} for supported methods and task config properties.
163 // Start a simple clock task that updates a div once per second
166 Ext.fly('clock').update(new Date().format('g:i:s A'));
168 interval: 1000 //1 second
170 Ext.TaskMgr.start(task);
174 Ext.TaskMgr = new Ext.util.TaskRunner();