3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.util.DelayedTask"></div>/**
15 * @class Ext.util.DelayedTask
16 * <p> The DelayedTask class provides a convenient way to "buffer" the execution of a method,
17 * performing setTimeout where a new timeout cancels the old timeout. When called, the
18 * task will wait the specified time period before executing. If durng that time period,
19 * the task is called again, the original call will be cancelled. This continues so that
20 * the function is only called a single time for each iteration.</p>
21 * <p>This method is especially useful for things like detecting whether a user has finished
22 * typing in a text field. An example would be performing validation on a keypress. You can
23 * use this class to buffer the keypress events for a certain number of milliseconds, and
24 * perform only if they stop for that amount of time. Usage:</p><pre><code>
25 var task = new Ext.util.DelayedTask(function(){
26 alert(Ext.getDom('myInputField').value.length);
28 // Wait 500ms before calling our function. If the user presses another key
29 // during that 500ms, it will be cancelled and we'll wait another 500ms.
30 Ext.get('myInputField').on('keypress', function(){
31 task.{@link #delay}(500);
34 * <p>Note that we are using a DelayedTask here to illustrate a point. The configuration
35 * option <tt>buffer</tt> for {@link Ext.util.Observable#addListener addListener/on} will
36 * also setup a delayed task for you to buffer events.</p>
37 * @constructor The parameters to this constructor serve as defaults and are not required.
38 * @param {Function} fn (optional) The default function to call.
39 * @param {Object} scope The default scope (The <code><b>this</b></code> reference) in which the
40 * function is called. If not specified, <code>this</code> will refer to the browser window.
41 * @param {Array} args (optional) The default Array of arguments.
43 Ext.util.DelayedTask = function(fn, scope, args){
49 fn.apply(scope, args || []);
52 <div id="method-Ext.util.DelayedTask-delay"></div>/**
53 * Cancels any pending timeout and queues a new one
54 * @param {Number} delay The milliseconds to delay
55 * @param {Function} newFn (optional) Overrides function passed to constructor
56 * @param {Object} newScope (optional) Overrides scope passed to constructor. Remember that if no scope
57 * is specified, <code>this</code> will refer to the browser window.
58 * @param {Array} newArgs (optional) Overrides args passed to constructor
60 me.delay = function(delay, newFn, newScope, newArgs){
63 scope = newScope || scope;
64 args = newArgs || args;
65 id = setInterval(call, delay);
68 <div id="method-Ext.util.DelayedTask-cancel"></div>/**
69 * Cancel the last queued timeout
71 me.cancel = function(){