4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../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 (optional) The minimum precision in milliseconds supported by this TaskRunner instance
53 Ext.util.TaskRunner = function(interval) {
54 interval = interval || 10;
61 stopThread = function() {
68 startThread = function() {
71 id = setInterval(runTasks, interval);
76 removeTask = function(t) {
79 t.onStop.apply(t.scope || t);
84 runTasks = function() {
85 var rqLen = removeQueue.length,
86 now = new Date().getTime(),
90 for (i = 0; i < rqLen; i++) {
91 Ext.Array.remove(tasks, removeQueue[i]);
94 if (tasks.length < 1) {
104 for (; i < len; ++i) {
106 itime = now - t.taskRunTime;
107 if (t.interval <= itime) {
108 rt = t.run.apply(t.scope || t, t.args || [++t.taskRunCount]);
110 if (rt === false || t.taskRunCount === t.repeat) {
115 if (t.duration && t.duration <= (now - t.taskStartTime)) {
121 <span id='Ext-util-TaskRunner-method-start'> /**
122 </span> * Starts a new task.
124 * @param {Object} task <p>A config object that supports the following properties:<ul>
125 * <li><code>run</code> : Function<div class="sub-desc"><p>The function to execute each time the task is invoked. The
126 * function will be called at each interval and passed the <code>args</code> argument if specified, and the
127 * current invocation count if not.</p>
128 * <p>If a particular scope (<code>this</code> reference) is required, be sure to specify it using the <code>scope</code> argument.</p>
129 * <p>Return <code>false</code> from this function to terminate the task.</p></div></li>
130 * <li><code>interval</code> : Number<div class="sub-desc">The frequency in milliseconds with which the task
131 * should be invoked.</div></li>
132 * <li><code>args</code> : Array<div class="sub-desc">(optional) An array of arguments to be passed to the function
133 * specified by <code>run</code>. If not specified, the current invocation count is passed.</div></li>
134 * <li><code>scope</code> : Object<div class="sub-desc">(optional) The scope (<tt>this</tt> reference) in which to execute the
135 * <code>run</code> function. Defaults to the task config object.</div></li>
136 * <li><code>duration</code> : Number<div class="sub-desc">(optional) The length of time in milliseconds to invoke
137 * the task before stopping automatically (defaults to indefinite).</div></li>
138 * <li><code>repeat</code> : Number<div class="sub-desc">(optional) The number of times to invoke the task before
139 * stopping automatically (defaults to indefinite).</div></li>
140 * </ul></p>
141 * <p>Before each invocation, Ext injects the property <code>taskRunCount</code> into the task object so
142 * that calculations based on the repeat count can be performed.</p>
143 * @return {Object} The task
145 this.start = function(task) {
147 task.taskStartTime = new Date().getTime();
148 task.taskRunTime = 0;
149 task.taskRunCount = 0;
154 <span id='Ext-util-TaskRunner-method-stop'> /**
155 </span> * Stops an existing running task.
157 * @param {Object} task The task to stop
158 * @return {Object} The task
160 this.stop = function(task) {
165 <span id='Ext-util-TaskRunner-method-stopAll'> /**
166 </span> * Stops all tasks that are currently running.
169 this.stopAll = function() {
171 for (var i = 0, len = tasks.length; i < len; i++) {
172 if (tasks[i].onStop) {
181 <span id='Ext-TaskManager'>/**
182 </span> * @class Ext.TaskManager
183 * @extends Ext.util.TaskRunner
184 * A static {@link Ext.util.TaskRunner} instance that can be used to start and stop arbitrary tasks. See
185 * {@link Ext.util.TaskRunner} for supported methods and task config properties.
186 * <pre><code>
187 // Start a simple clock task that updates a div once per second
190 Ext.fly('clock').update(new Date().format('g:i:s A'));
192 interval: 1000 //1 second
194 Ext.TaskManager.start(task);
195 </code></pre>
196 * <p>See the {@link #start} method for details about how to configure a task object.</p>
199 Ext.TaskManager = Ext.create('Ext.util.TaskRunner');</pre>