You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
3.8 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-util-DelayedTask-method-constructor'><span id='Ext-util-DelayedTask'>/**
  19. </span></span> * @class Ext.util.DelayedTask
  20. * &lt;p&gt; The DelayedTask class provides a convenient way to &quot;buffer&quot; the execution of a method,
  21. * performing setTimeout where a new timeout cancels the old timeout. When called, the
  22. * task will wait the specified time period before executing. If durng that time period,
  23. * the task is called again, the original call will be cancelled. This continues so that
  24. * the function is only called a single time for each iteration.&lt;/p&gt;
  25. * &lt;p&gt;This method is especially useful for things like detecting whether a user has finished
  26. * typing in a text field. An example would be performing validation on a keypress. You can
  27. * use this class to buffer the keypress events for a certain number of milliseconds, and
  28. * perform only if they stop for that amount of time. Usage:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;
  29. var task = new Ext.util.DelayedTask(function(){
  30. alert(Ext.getDom('myInputField').value.length);
  31. });
  32. // Wait 500ms before calling our function. If the user presses another key
  33. // during that 500ms, it will be cancelled and we'll wait another 500ms.
  34. Ext.get('myInputField').on('keypress', function(){
  35. task.{@link #delay}(500);
  36. });
  37. * &lt;/code&gt;&lt;/pre&gt;
  38. * &lt;p&gt;Note that we are using a DelayedTask here to illustrate a point. The configuration
  39. * option &lt;tt&gt;buffer&lt;/tt&gt; for {@link Ext.util.Observable#addListener addListener/on} will
  40. * also setup a delayed task for you to buffer events.&lt;/p&gt;
  41. * @constructor The parameters to this constructor serve as defaults and are not required.
  42. * @param {Function} fn (optional) The default function to call.
  43. * @param {Object} scope (optional) The default scope (The &lt;code&gt;&lt;b&gt;this&lt;/b&gt;&lt;/code&gt; reference) in which the
  44. * function is called. If not specified, &lt;code&gt;this&lt;/code&gt; will refer to the browser window.
  45. * @param {Array} args (optional) The default Array of arguments.
  46. */
  47. Ext.util.DelayedTask = function(fn, scope, args){
  48. var me = this,
  49. id,
  50. call = function(){
  51. clearInterval(id);
  52. id = null;
  53. fn.apply(scope, args || []);
  54. };
  55. <span id='Ext-util-DelayedTask-method-delay'> /**
  56. </span> * Cancels any pending timeout and queues a new one
  57. * @param {Number} delay The milliseconds to delay
  58. * @param {Function} newFn (optional) Overrides function passed to constructor
  59. * @param {Object} newScope (optional) Overrides scope passed to constructor. Remember that if no scope
  60. * is specified, &lt;code&gt;this&lt;/code&gt; will refer to the browser window.
  61. * @param {Array} newArgs (optional) Overrides args passed to constructor
  62. */
  63. me.delay = function(delay, newFn, newScope, newArgs){
  64. me.cancel();
  65. fn = newFn || fn;
  66. scope = newScope || scope;
  67. args = newArgs || args;
  68. id = setInterval(call, delay);
  69. };
  70. <span id='Ext-util-DelayedTask-method-cancel'> /**
  71. </span> * Cancel the last queued timeout
  72. */
  73. me.cancel = function(){
  74. if(id){
  75. clearInterval(id);
  76. id = null;
  77. }
  78. };
  79. };</pre>
  80. </body>
  81. </html>