3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.util.TaskRunner"></div>/**
15 * @class Ext.util.TaskRunner
16 * Provides the ability to execute one or more arbitrary tasks in a multithreaded
17 * manner. Generally, you can use the singleton {@link Ext.TaskMgr} instead, but
18 * if needed, you can create separate instances of TaskRunner. Any number of
19 * separate tasks can be started at any time and will run independently of each
20 * other. Example usage:
22 // Start a simple clock task that updates a div once per second
23 var updateClock = function(){
24 Ext.fly('clock').update(new Date().format('g:i:s A'));
28 interval: 1000 //1 second
30 var runner = new Ext.util.TaskRunner();
33 // equivalent using TaskMgr
40 * Also see {@link Ext.util.DelayedTask}.
43 * @param {Number} interval (optional) The minimum precision in milliseconds supported by this TaskRunner instance
46 Ext.util.TaskRunner = function(interval){
47 interval = interval || 10;
54 stopThread = function(){
61 startThread = function(){
64 id = setInterval(runTasks, interval);
69 removeTask = function(t){
72 t.onStop.apply(t.scope || t);
77 runTasks = function(){
78 var rqLen = removeQueue.length,
79 now = new Date().getTime();
82 for(var i = 0; i < rqLen; i++){
83 tasks.remove(removeQueue[i]);
91 for(var i = 0, t, itime, rt, len = tasks.length; i < len; ++i){
93 itime = now - t.taskRunTime;
94 if(t.interval <= itime){
95 rt = t.run.apply(t.scope || t, t.args || [++t.taskRunCount]);
97 if(rt === false || t.taskRunCount === t.repeat){
102 if(t.duration && t.duration <= (now - t.taskStartTime)){
108 <div id="method-Ext.util.TaskRunner-start"></div>/**
111 * @param {Object} task A config object that supports the following properties:<ul>
112 * <li><code>run</code> : Function<div class="sub-desc">The function to execute each time the task is run. The
113 * function will be called at each interval and passed the <code>args</code> argument if specified. If a
114 * particular scope is required, be sure to specify it using the <code>scope</code> argument.</div></li>
115 * <li><code>interval</code> : Number<div class="sub-desc">The frequency in milliseconds with which the task
116 * should be executed.</div></li>
117 * <li><code>args</code> : Array<div class="sub-desc">(optional) An array of arguments to be passed to the function
118 * specified by <code>run</code>.</div></li>
119 * <li><code>scope</code> : Object<div class="sub-desc">(optional) The scope (<tt>this</tt> reference) in which to execute the
120 * <code>run</code> function. Defaults to the task config object.</div></li>
121 * <li><code>duration</code> : Number<div class="sub-desc">(optional) The length of time in milliseconds to execute
122 * the task before stopping automatically (defaults to indefinite).</div></li>
123 * <li><code>repeat</code> : Number<div class="sub-desc">(optional) The number of times to execute the task before
124 * stopping automatically (defaults to indefinite).</div></li>
126 * @return {Object} The task
128 this.start = function(task){
130 task.taskStartTime = new Date().getTime();
131 task.taskRunTime = 0;
132 task.taskRunCount = 0;
137 <div id="method-Ext.util.TaskRunner-stop"></div>/**
138 * Stops an existing running task.
140 * @param {Object} task The task to stop
141 * @return {Object} The task
143 this.stop = function(task){
148 <div id="method-Ext.util.TaskRunner-stopAll"></div>/**
149 * Stops all tasks that are currently running.
152 this.stopAll = function(){
154 for(var i = 0, len = tasks.length; i < len; i++){
164 <div id="cls-Ext.TaskMgr"></div>/**
166 * @extends Ext.util.TaskRunner
167 * A static {@link Ext.util.TaskRunner} instance that can be used to start and stop arbitrary tasks. See
168 * {@link Ext.util.TaskRunner} for supported methods and task config properties.
170 // Start a simple clock task that updates a div once per second
173 Ext.fly('clock').update(new Date().format('g:i:s A'));
175 interval: 1000 //1 second
177 Ext.TaskMgr.start(task);
181 Ext.TaskMgr = new Ext.util.TaskRunner();</pre>