Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / core / src / util / DelayedTask.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.util.DelayedTask
17  * 
18  * The DelayedTask class provides a convenient way to "buffer" the execution of a method,
19  * performing setTimeout where a new timeout cancels the old timeout. When called, the
20  * task will wait the specified time period before executing. If durng that time period,
21  * the task is called again, the original call will be cancelled. This continues so that
22  * the function is only called a single time for each iteration.
23  * 
24  * This method is especially useful for things like detecting whether a user has finished
25  * typing in a text field. An example would be performing validation on a keypress. You can
26  * use this class to buffer the keypress events for a certain number of milliseconds, and
27  * perform only if they stop for that amount of time.  
28  * 
29  * ## Usage
30  * 
31  *     var task = new Ext.util.DelayedTask(function(){
32  *         alert(Ext.getDom('myInputField').value.length);
33  *     });
34  *     
35  *     // Wait 500ms before calling our function. If the user presses another key
36  *     // during that 500ms, it will be cancelled and we'll wait another 500ms.
37  *     Ext.get('myInputField').on('keypress', function(){
38  *         task.{@link #delay}(500);
39  *     });
40  * 
41  * Note that we are using a DelayedTask here to illustrate a point. The configuration
42  * option `buffer` for {@link Ext.util.Observable#addListener addListener/on} will
43  * also setup a delayed task for you to buffer events.
44  * 
45  * @constructor The parameters to this constructor serve as defaults and are not required.
46  * @param {Function} fn (optional) The default function to call. If not specified here, it must be specified during the {@link #delay} call.
47  * @param {Object} scope (optional) The default scope (The <code><b>this</b></code> reference) in which the
48  * function is called. If not specified, <code>this</code> will refer to the browser window.
49  * @param {Array} args (optional) The default Array of arguments.
50  */
51 Ext.util.DelayedTask = function(fn, scope, args) {
52     var me = this,
53         id,
54         call = function() {
55             clearInterval(id);
56             id = null;
57             fn.apply(scope, args || []);
58         };
59
60     /**
61      * Cancels any pending timeout and queues a new one
62      * @param {Number} delay The milliseconds to delay
63      * @param {Function} newFn (optional) Overrides function passed to constructor
64      * @param {Object} newScope (optional) Overrides scope passed to constructor. Remember that if no scope
65      * is specified, <code>this</code> will refer to the browser window.
66      * @param {Array} newArgs (optional) Overrides args passed to constructor
67      */
68     this.delay = function(delay, newFn, newScope, newArgs) {
69         me.cancel();
70         fn = newFn || fn;
71         scope = newScope || scope;
72         args = newArgs || args;
73         id = setInterval(call, delay);
74     };
75
76     /**
77      * Cancel the last queued timeout
78      */
79     this.cancel = function(){
80         if (id) {
81             clearInterval(id);
82             id = null;
83         }
84     };
85 };