3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.util.TaskRunner"></div>/**
10 * @class Ext.util.TaskRunner
11 * Provides the ability to execute one or more arbitrary tasks in a multithreaded
12 * manner. Generally, you can use the singleton {@link Ext.TaskMgr} instead, but
13 * if needed, you can create separate instances of TaskRunner. Any number of
14 * separate tasks can be started at any time and will run independently of each
15 * other. Example usage:
17 // Start a simple clock task that updates a div once per second
18 var updateClock = function(){
19 Ext.fly('clock').update(new Date().format('g:i:s A'));
23 interval: 1000 //1 second
25 var runner = new Ext.util.TaskRunner();
28 // equivalent using TaskMgr
35 * Also see {@link Ext.util.DelayedTask}.
38 * @param {Number} interval (optional) The minimum precision in milliseconds supported by this TaskRunner instance
41 Ext.util.TaskRunner = function(interval){
42 interval = interval || 10;
49 stopThread = function(){
56 startThread = function(){
59 id = setInterval(runTasks, interval);
64 removeTask = function(t){
67 t.onStop.apply(t.scope || t);
72 runTasks = function(){
73 var rqLen = removeQueue.length,
74 now = new Date().getTime();
77 for(var i = 0; i < rqLen; i++){
78 tasks.remove(removeQueue[i]);
86 for(var i = 0, t, itime, rt, len = tasks.length; i < len; ++i){
88 itime = now - t.taskRunTime;
89 if(t.interval <= itime){
90 rt = t.run.apply(t.scope || t, t.args || [++t.taskRunCount]);
92 if(rt === false || t.taskRunCount === t.repeat){
97 if(t.duration && t.duration <= (now - t.taskStartTime)){
103 <div id="method-Ext.util.TaskRunner-start"></div>/**
106 * @param {Object} task A config object that supports the following properties:<ul>
107 * <li><code>run</code> : Function<div class="sub-desc">The function to execute each time the task is run. The
108 * function will be called at each interval and passed the <code>args</code> argument if specified. If a
109 * particular scope is required, be sure to specify it using the <code>scope</code> argument.</div></li>
110 * <li><code>interval</code> : Number<div class="sub-desc">The frequency in milliseconds with which the task
111 * should be executed.</div></li>
112 * <li><code>args</code> : Array<div class="sub-desc">(optional) An array of arguments to be passed to the function
113 * specified by <code>run</code>.</div></li>
114 * <li><code>scope</code> : Object<div class="sub-desc">(optional) The scope (<tt>this</tt> reference) in which to execute the
115 * <code>run</code> function. Defaults to the task config object.</div></li>
116 * <li><code>duration</code> : Number<div class="sub-desc">(optional) The length of time in milliseconds to execute
117 * the task before stopping automatically (defaults to indefinite).</div></li>
118 * <li><code>repeat</code> : Number<div class="sub-desc">(optional) The number of times to execute the task before
119 * stopping automatically (defaults to indefinite).</div></li>
121 * @return {Object} The task
123 this.start = function(task){
125 task.taskStartTime = new Date().getTime();
126 task.taskRunTime = 0;
127 task.taskRunCount = 0;
132 <div id="method-Ext.util.TaskRunner-stop"></div>/**
133 * Stops an existing running task.
135 * @param {Object} task The task to stop
136 * @return {Object} The task
138 this.stop = function(task){
143 <div id="method-Ext.util.TaskRunner-stopAll"></div>/**
144 * Stops all tasks that are currently running.
147 this.stopAll = function(){
149 for(var i = 0, len = tasks.length; i < len; i++){
159 <div id="cls-Ext.TaskMgr"></div>/**
161 * @extends Ext.util.TaskRunner
162 * A static {@link Ext.util.TaskRunner} instance that can be used to start and stop arbitrary tasks. See
163 * {@link Ext.util.TaskRunner} for supported methods and task config properties.
165 // Start a simple clock task that updates a div once per second
168 Ext.fly('clock').update(new Date().format('g:i:s A'));
170 interval: 1000 //1 second
172 Ext.TaskMgr.start(task);
176 Ext.TaskMgr = new Ext.util.TaskRunner();</pre>
\r