3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.3.1
11 * Copyright(c) 2006-2010 Sencha Inc.
12 * licensing@sencha.com
13 * http://www.sencha.com/license
15 <div id="cls-Ext.util.TaskRunner"></div>/**
16 * @class Ext.util.TaskRunner
17 * Provides the ability to execute one or more arbitrary tasks in a multithreaded
18 * manner. Generally, you can use the singleton {@link Ext.TaskMgr} instead, but
19 * if needed, you can create separate instances of TaskRunner. Any number of
20 * separate tasks can be started at any time and will run independently of each
21 * other. Example usage:
23 // Start a simple clock task that updates a div once per second
24 var updateClock = function(){
25 Ext.fly('clock').update(new Date().format('g:i:s A'));
29 interval: 1000 //1 second
31 var runner = new Ext.util.TaskRunner();
34 // equivalent using TaskMgr
41 * <p>See the {@link #start} method for details about how to configure a task object.</p>
42 * Also see {@link Ext.util.DelayedTask}.
45 * @param {Number} interval (optional) The minimum precision in milliseconds supported by this TaskRunner instance
48 Ext.util.TaskRunner = function(interval){
49 interval = interval || 10;
56 stopThread = function(){
63 startThread = function(){
66 id = setInterval(runTasks, interval);
71 removeTask = function(t){
74 t.onStop.apply(t.scope || t);
79 runTasks = function(){
80 var rqLen = removeQueue.length,
81 now = new Date().getTime();
84 for(var i = 0; i < rqLen; i++){
85 tasks.remove(removeQueue[i]);
93 for(var i = 0, t, itime, rt, len = tasks.length; i < len; ++i){
95 itime = now - t.taskRunTime;
96 if(t.interval <= itime){
97 rt = t.run.apply(t.scope || t, t.args || [++t.taskRunCount]);
99 if(rt === false || t.taskRunCount === t.repeat){
104 if(t.duration && t.duration <= (now - t.taskStartTime)){
110 <div id="method-Ext.util.TaskRunner-start"></div>/**
113 * @param {Object} task <p>A config object that supports the following properties:<ul>
114 * <li><code>run</code> : Function<div class="sub-desc"><p>The function to execute each time the task is invoked. The
115 * function will be called at each interval and passed the <code>args</code> argument if specified, and the
116 * current invocation count if not.</p>
117 * <p>If a particular scope (<code>this</code> reference) is required, be sure to specify it using the <code>scope</code> argument.</p>
118 * <p>Return <code>false</code> from this function to terminate the task.</p></div></li>
119 * <li><code>interval</code> : Number<div class="sub-desc">The frequency in milliseconds with which the task
120 * should be invoked.</div></li>
121 * <li><code>args</code> : Array<div class="sub-desc">(optional) An array of arguments to be passed to the function
122 * specified by <code>run</code>. If not specified, the current invocation count is passed.</div></li>
123 * <li><code>scope</code> : Object<div class="sub-desc">(optional) The scope (<tt>this</tt> reference) in which to execute the
124 * <code>run</code> function. Defaults to the task config object.</div></li>
125 * <li><code>duration</code> : Number<div class="sub-desc">(optional) The length of time in milliseconds to invoke
126 * the task before stopping automatically (defaults to indefinite).</div></li>
127 * <li><code>repeat</code> : Number<div class="sub-desc">(optional) The number of times to invoke the task before
128 * stopping automatically (defaults to indefinite).</div></li>
130 * <p>Before each invocation, Ext injects the property <code>taskRunCount</code> into the task object so
131 * that calculations based on the repeat count can be performed.</p>
132 * @return {Object} The task
134 this.start = function(task){
136 task.taskStartTime = new Date().getTime();
137 task.taskRunTime = 0;
138 task.taskRunCount = 0;
143 <div id="method-Ext.util.TaskRunner-stop"></div>/**
144 * Stops an existing running task.
146 * @param {Object} task The task to stop
147 * @return {Object} The task
149 this.stop = function(task){
154 <div id="method-Ext.util.TaskRunner-stopAll"></div>/**
155 * Stops all tasks that are currently running.
158 this.stopAll = function(){
160 for(var i = 0, len = tasks.length; i < len; i++){
170 <div id="cls-Ext.TaskMgr"></div>/**
172 * @extends Ext.util.TaskRunner
173 * A static {@link Ext.util.TaskRunner} instance that can be used to start and stop arbitrary tasks. See
174 * {@link Ext.util.TaskRunner} for supported methods and task config properties.
176 // Start a simple clock task that updates a div once per second
179 Ext.fly('clock').update(new Date().format('g:i:s A'));
181 interval: 1000 //1 second
183 Ext.TaskMgr.start(task);
185 * <p>See the {@link #start} method for details about how to configure a task object.</p>
188 Ext.TaskMgr = new Ext.util.TaskRunner();</pre>