1 | /* |
---|
2 | * Ext JS Library 2.2.1 |
---|
3 | * Copyright(c) 2006-2009, Ext JS, LLC. |
---|
4 | * licensing@extjs.com |
---|
5 | * |
---|
6 | * http://extjs.com/license |
---|
7 | */ |
---|
8 | |
---|
9 | /** |
---|
10 | * @class Ext.util.DelayedTask |
---|
11 | * Provides a convenient method of performing setTimeout where a new |
---|
12 | * timeout cancels the old timeout. An example would be performing validation on a keypress. |
---|
13 | * You can use this class to buffer |
---|
14 | * the keypress events for a certain number of milliseconds, and perform only if they stop |
---|
15 | * for that amount of time. |
---|
16 | * @constructor The parameters to this constructor serve as defaults and are not required. |
---|
17 | * @param {Function} fn (optional) The default function to timeout |
---|
18 | * @param {Object} scope (optional) The default scope of that timeout |
---|
19 | * @param {Array} args (optional) The default Array of arguments |
---|
20 | */ |
---|
21 | Ext.util.DelayedTask = function(fn, scope, args){ |
---|
22 | var id = null, d, t; |
---|
23 | |
---|
24 | var call = function(){ |
---|
25 | var now = new Date().getTime(); |
---|
26 | if(now - t >= d){ |
---|
27 | clearInterval(id); |
---|
28 | id = null; |
---|
29 | fn.apply(scope, args || []); |
---|
30 | } |
---|
31 | }; |
---|
32 | /** |
---|
33 | * Cancels any pending timeout and queues a new one |
---|
34 | * @param {Number} delay The milliseconds to delay |
---|
35 | * @param {Function} newFn (optional) Overrides function passed to constructor |
---|
36 | * @param {Object} newScope (optional) Overrides scope passed to constructor |
---|
37 | * @param {Array} newArgs (optional) Overrides args passed to constructor |
---|
38 | */ |
---|
39 | this.delay = function(delay, newFn, newScope, newArgs){ |
---|
40 | if(id && delay != d){ |
---|
41 | this.cancel(); |
---|
42 | } |
---|
43 | d = delay; |
---|
44 | t = new Date().getTime(); |
---|
45 | fn = newFn || fn; |
---|
46 | scope = newScope || scope; |
---|
47 | args = newArgs || args; |
---|
48 | if(!id){ |
---|
49 | id = setInterval(call, d); |
---|
50 | } |
---|
51 | }; |
---|
52 | |
---|
53 | /** |
---|
54 | * Cancel the last queued timeout |
---|
55 | */ |
---|
56 | this.cancel = function(){ |
---|
57 | if(id){ |
---|
58 | clearInterval(id); |
---|
59 | id = null; |
---|
60 | } |
---|
61 | }; |
---|
62 | }; |
---|