4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-util-TaskRunner-method-constructor'><span id='Ext-util-TaskRunner'>/**
19 </span></span> * @class Ext.util.TaskRunner
20 * Provides the ability to execute one or more arbitrary tasks in a multithreaded
21 * manner. Generally, you can use the singleton {@link Ext.TaskManager} instead, but
22 * if needed, you can create separate instances of TaskRunner. Any number of
23 * separate tasks can be started at any time and will run independently of each
24 * other. Example usage:
25 * <pre><code>
26 // Start a simple clock task that updates a div once per second
27 var updateClock = function(){
28 Ext.fly('clock').update(new Date().format('g:i:s A'));
32 interval: 1000 //1 second
34 var runner = new Ext.util.TaskRunner();
37 // equivalent using TaskManager
38 Ext.TaskManager.start({
43 * </code></pre>
44 * <p>See the {@link #start} method for details about how to configure a task object.</p>
45 * Also see {@link Ext.util.DelayedTask}.
48 * @param {Number} [interval=10] The minimum precision in milliseconds supported by this TaskRunner instance
52 Ext.util.TaskRunner = function(interval) {
53 interval = interval || 10;
60 stopThread = function() {
67 startThread = function() {
70 id = setInterval(runTasks, interval);
75 removeTask = function(t) {
78 t.onStop.apply(t.scope || t);
83 runTasks = function() {
84 var rqLen = removeQueue.length,
85 now = new Date().getTime(),
89 for (i = 0; i < rqLen; i++) {
90 Ext.Array.remove(tasks, removeQueue[i]);
93 if (tasks.length < 1) {
103 for (; i < len; ++i) {
105 itime = now - t.taskRunTime;
106 if (t.interval <= itime) {
107 rt = t.run.apply(t.scope || t, t.args || [++t.taskRunCount]);
109 if (rt === false || t.taskRunCount === t.repeat) {
114 if (t.duration && t.duration <= (now - t.taskStartTime)) {
120 <span id='Ext-util-TaskRunner-method-start'> /**
121 </span> * Starts a new task.
123 * @param {Object} task <p>A config object that supports the following properties:<ul>
124 * <li><code>run</code> : Function<div class="sub-desc"><p>The function to execute each time the task is invoked. The
125 * function will be called at each interval and passed the <code>args</code> argument if specified, and the
126 * current invocation count if not.</p>
127 * <p>If a particular scope (<code>this</code> reference) is required, be sure to specify it using the <code>scope</code> argument.</p>
128 * <p>Return <code>false</code> from this function to terminate the task.</p></div></li>
129 * <li><code>interval</code> : Number<div class="sub-desc">The frequency in milliseconds with which the task
130 * should be invoked.</div></li>
131 * <li><code>args</code> : Array<div class="sub-desc">(optional) An array of arguments to be passed to the function
132 * specified by <code>run</code>. If not specified, the current invocation count is passed.</div></li>
133 * <li><code>scope</code> : Object<div class="sub-desc">(optional) The scope (<tt>this</tt> reference) in which to execute the
134 * <code>run</code> function. Defaults to the task config object.</div></li>
135 * <li><code>duration</code> : Number<div class="sub-desc">(optional) The length of time in milliseconds to invoke
136 * the task before stopping automatically (defaults to indefinite).</div></li>
137 * <li><code>repeat</code> : Number<div class="sub-desc">(optional) The number of times to invoke the task before
138 * stopping automatically (defaults to indefinite).</div></li>
139 * </ul></p>
140 * <p>Before each invocation, Ext injects the property <code>taskRunCount</code> into the task object so
141 * that calculations based on the repeat count can be performed.</p>
142 * @return {Object} The task
144 this.start = function(task) {
146 task.taskStartTime = new Date().getTime();
147 task.taskRunTime = 0;
148 task.taskRunCount = 0;
153 <span id='Ext-util-TaskRunner-method-stop'> /**
154 </span> * Stops an existing running task.
156 * @param {Object} task The task to stop
157 * @return {Object} The task
159 this.stop = function(task) {
164 <span id='Ext-util-TaskRunner-method-stopAll'> /**
165 </span> * Stops all tasks that are currently running.
168 this.stopAll = function() {
170 for (var i = 0, len = tasks.length; i < len; i++) {
171 if (tasks[i].onStop) {
180 <span id='Ext-TaskManager'>/**
181 </span> * @class Ext.TaskManager
182 * @extends Ext.util.TaskRunner
183 * A static {@link Ext.util.TaskRunner} instance that can be used to start and stop arbitrary tasks. See
184 * {@link Ext.util.TaskRunner} for supported methods and task config properties.
185 * <pre><code>
186 // Start a simple clock task that updates a div once per second
189 Ext.fly('clock').update(new Date().format('g:i:s A'));
191 interval: 1000 //1 second
193 Ext.TaskManager.start(task);
194 </code></pre>
195 * <p>See the {@link #start} method for details about how to configure a task object.</p>
198 Ext.TaskManager = Ext.create('Ext.util.TaskRunner');</pre>