1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-util.TaskRunner-method-constructor'><span id='Ext-util.TaskRunner'>/**
2 </span></span> * @class Ext.util.TaskRunner
3 * Provides the ability to execute one or more arbitrary tasks in a multithreaded
4 * manner. Generally, you can use the singleton {@link Ext.TaskManager} instead, but
5 * if needed, you can create separate instances of TaskRunner. Any number of
6 * separate tasks can be started at any time and will run independently of each
7 * other. Example usage:
8 * <pre><code>
9 // Start a simple clock task that updates a div once per second
10 var updateClock = function(){
11 Ext.fly('clock').update(new Date().format('g:i:s A'));
15 interval: 1000 //1 second
17 var runner = new Ext.util.TaskRunner();
20 // equivalent using TaskManager
21 Ext.TaskManager.start({
26 * </code></pre>
27 * <p>See the {@link #start} method for details about how to configure a task object.</p>
28 * Also see {@link Ext.util.DelayedTask}.
31 * @param {Number} interval (optional) The minimum precision in milliseconds supported by this TaskRunner instance
36 Ext.util.TaskRunner = function(interval) {
37 interval = interval || 10;
44 stopThread = function() {
51 startThread = function() {
54 id = setInterval(runTasks, interval);
59 removeTask = function(t) {
62 t.onStop.apply(t.scope || t);
67 runTasks = function() {
68 var rqLen = removeQueue.length,
69 now = new Date().getTime(),
73 for (i = 0; i < rqLen; i++) {
74 Ext.Array.remove(tasks, removeQueue[i]);
77 if (tasks.length < 1) {
87 for (; i < len; ++i) {
89 itime = now - t.taskRunTime;
90 if (t.interval <= itime) {
91 rt = t.run.apply(t.scope || t, t.args || [++t.taskRunCount]);
93 if (rt === false || t.taskRunCount === t.repeat) {
98 if (t.duration && t.duration <= (now - t.taskStartTime)) {
104 <span id='Ext-util.TaskRunner-method-start'> /**
105 </span> * Starts a new task.
107 * @param {Object} task <p>A config object that supports the following properties:<ul>
108 * <li><code>run</code> : Function<div class="sub-desc"><p>The function to execute each time the task is invoked. The
109 * function will be called at each interval and passed the <code>args</code> argument if specified, and the
110 * current invocation count if not.</p>
111 * <p>If a particular scope (<code>this</code> reference) is required, be sure to specify it using the <code>scope</code> argument.</p>
112 * <p>Return <code>false</code> from this function to terminate the task.</p></div></li>
113 * <li><code>interval</code> : Number<div class="sub-desc">The frequency in milliseconds with which the task
114 * should be invoked.</div></li>
115 * <li><code>args</code> : Array<div class="sub-desc">(optional) An array of arguments to be passed to the function
116 * specified by <code>run</code>. If not specified, the current invocation count is passed.</div></li>
117 * <li><code>scope</code> : Object<div class="sub-desc">(optional) The scope (<tt>this</tt> reference) in which to execute the
118 * <code>run</code> function. Defaults to the task config object.</div></li>
119 * <li><code>duration</code> : Number<div class="sub-desc">(optional) The length of time in milliseconds to invoke
120 * the task before stopping automatically (defaults to indefinite).</div></li>
121 * <li><code>repeat</code> : Number<div class="sub-desc">(optional) The number of times to invoke the task before
122 * stopping automatically (defaults to indefinite).</div></li>
123 * </ul></p>
124 * <p>Before each invocation, Ext injects the property <code>taskRunCount</code> into the task object so
125 * that calculations based on the repeat count can be performed.</p>
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 <span id='Ext-util.TaskRunner-method-stop'> /**
138 </span> * Stops an existing running task.
140 * @param {Object} task The task to stop
141 * @return {Object} The task
143 this.stop = function(task) {
148 <span id='Ext-util.TaskRunner-method-stopAll'> /**
149 </span> * Stops all tasks that are currently running.
152 this.stopAll = function() {
154 for (var i = 0, len = tasks.length; i < len; i++) {
155 if (tasks[i].onStop) {
164 <span id='Ext-TaskManager'>/**
165 </span> * @class Ext.TaskManager
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.
169 * <pre><code>
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.TaskManager.start(task);
178 </code></pre>
179 * <p>See the {@link #start} method for details about how to configure a task object.</p>
182 Ext.TaskManager = Ext.create('Ext.util.TaskRunner');</pre></pre></body></html>