3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.util.TaskRunner"></div>/**
9 * @class Ext.util.TaskRunner
10 * Provides the ability to execute one or more arbitrary tasks in a multithreaded
11 * manner. Generally, you can use the singleton {@link Ext.TaskMgr} instead, but
12 * if needed, you can create separate instances of TaskRunner. Any number of
13 * separate tasks can be started at any time and will run independently of each
14 * other. Example usage:
16 // Start a simple clock task that updates a div once per second
17 var updateClock = function(){
18 Ext.fly('clock').update(new Date().format('g:i:s A'));
22 interval: 1000 //1 second
24 var runner = new Ext.util.TaskRunner();
27 // equivalent using TaskMgr
34 * Also see {@link Ext.util.DelayedTask}.
37 * @param {Number} interval (optional) The minimum precision in milliseconds supported by this TaskRunner instance
40 Ext.util.TaskRunner = function(interval){
41 interval = interval || 10;
48 stopThread = function(){
55 startThread = function(){
58 id = setInterval(runTasks, interval);
63 removeTask = function(t){
66 t.onStop.apply(t.scope || t);
71 runTasks = function(){
72 var rqLen = removeQueue.length,
73 now = new Date().getTime();
76 for(var i = 0; i < rqLen; i++){
77 tasks.remove(removeQueue[i]);
85 for(var i = 0, t, itime, rt, len = tasks.length; i < len; ++i){
87 itime = now - t.taskRunTime;
88 if(t.interval <= itime){
89 rt = t.run.apply(t.scope || t, t.args || [++t.taskRunCount]);
91 if(rt === false || t.taskRunCount === t.repeat){
96 if(t.duration && t.duration <= (now - t.taskStartTime)){
102 <div id="method-Ext.util.TaskRunner-start"></div>/**
105 * @param {Object} task A config object that supports the following properties:<ul>
106 * <li><code>run</code> : Function<div class="sub-desc">The function to execute each time the task is run. The
107 * function will be called at each interval and passed the <code>args</code> argument if specified. If a
108 * particular scope is required, be sure to specify it using the <code>scope</code> argument.</div></li>
109 * <li><code>interval</code> : Number<div class="sub-desc">The frequency in milliseconds with which the task
110 * should be executed.</div></li>
111 * <li><code>args</code> : Array<div class="sub-desc">(optional) An array of arguments to be passed to the function
112 * specified by <code>run</code>.</div></li>
113 * <li><code>scope</code> : Object<div class="sub-desc">(optional) The scope (<tt>this</tt> reference) in which to execute the
114 * <code>run</code> function. Defaults to the task config object.</div></li>
115 * <li><code>duration</code> : Number<div class="sub-desc">(optional) The length of time in milliseconds to execute
116 * the task before stopping automatically (defaults to indefinite).</div></li>
117 * <li><code>repeat</code> : Number<div class="sub-desc">(optional) The number of times to execute the task before
118 * stopping automatically (defaults to indefinite).</div></li>
120 * @return {Object} The task
122 this.start = function(task){
124 task.taskStartTime = new Date().getTime();
125 task.taskRunTime = 0;
126 task.taskRunCount = 0;
131 <div id="method-Ext.util.TaskRunner-stop"></div>/**
132 * Stops an existing running task.
134 * @param {Object} task The task to stop
135 * @return {Object} The task
137 this.stop = function(task){
142 <div id="method-Ext.util.TaskRunner-stopAll"></div>/**
143 * Stops all tasks that are currently running.
146 this.stopAll = function(){
148 for(var i = 0, len = tasks.length; i < len; i++){
158 <div id="cls-Ext.TaskMgr"></div>/**
160 * @extends Ext.util.TaskRunner
161 * A static {@link Ext.util.TaskRunner} instance that can be used to start and stop arbitrary tasks. See
162 * {@link Ext.util.TaskRunner} for supported methods and task config properties.
164 // Start a simple clock task that updates a div once per second
167 Ext.fly('clock').update(new Date().format('g:i:s A'));
169 interval: 1000 //1 second
171 Ext.TaskMgr.start(task);
175 Ext.TaskMgr = new Ext.util.TaskRunner();</pre>
\r