Show:
  1. /**
  2. * The DelayedTask Utility - Executes the supplied function in the context of
  3. * the supplied object 'when' milliseconds later
  4. *
  5. * @module aui-delayed-task
  6. */
  7. /**
  8. * A base class for DelayedTask, providing:
  9. * <ul>
  10. * <li>Executes the supplied function in the context of the supplied object 'when' milliseconds later</li>
  11. * </ul>
  12. *
  13. * Quick Example:<br/>
  14. *
  15. * <pre><code>var delayed = new A.DelayedTask({
  16. * function() {
  17. * // This callback will be executed when the <code>DelayedTask</code> be invoked
  18. * },
  19. * scope
  20. * });
  21. *
  22. * // executes after 1000ms the callback
  23. * delayed.delay(1000);
  24. * </code></pre>
  25. *
  26. * Check the list of <a href="DelayedTask.html#configattributes">Configuration Attributes</a> available for
  27. * DelayedTask.
  28. *
  29. * @param config {Object} Object literal specifying widget configuration properties.
  30. *
  31. * @class DelayedTask
  32. * @param {function} fn Callback
  33. * @param {Object} scope Context object. Optional.
  34. * @param args 0..n additional arguments that should be provided to the listener.
  35. * @constructor
  36. */
  37. var DelayedTask = function(fn, scope, args) {
  38. var instance = this;
  39. /**
  40. * Stores the passed <code>args</code> attribute.
  41. *
  42. * @property _args
  43. * @type Object
  44. * @protected
  45. */
  46. instance._args = args;
  47. /**
  48. * Stores the passed <code>delay</code> attribute.
  49. *
  50. * @property _delay
  51. * @default 0
  52. * @type Number
  53. * @protected
  54. */
  55. instance._delay = 0;
  56. /**
  57. * Stores the passed <code>fn</code> attribute.
  58. *
  59. * @property _fn
  60. * @type function
  61. * @protected
  62. */
  63. instance._fn = fn;
  64. /**
  65. * Stores the timer <code>id</code> given from the <code>setInterval</code>.
  66. *
  67. * @property _id
  68. * @default null
  69. * @type Number
  70. * @protected
  71. */
  72. instance._id = null;
  73. /**
  74. * Stores the passed <code>scope</code> attribute.
  75. *
  76. * @property _scope
  77. * @default instance
  78. * @type Object
  79. * @protected
  80. */
  81. instance._scope = scope || instance;
  82. /**
  83. * Stores the current timestamp given from
  84. * <a href="DelayedTask.html#method__getTime">_getTime</a>.
  85. *
  86. * @property _time
  87. * @default 0
  88. * @type Number
  89. * @protected
  90. */
  91. instance._time = 0;
  92. instance._base = function() {
  93. var now = instance._getTime();
  94. if (now - instance._time >= instance._delay) {
  95. clearInterval(instance._id);
  96. instance._id = null;
  97. instance._fn.apply(instance._scope, instance._args || []);
  98. }
  99. };
  100. };
  101. DelayedTask.prototype = {
  102. /**
  103. * <p>This function is responsible to execute the user callback, passed in
  104. * the <code>constructor</code> after <code>delay</code> milliseconds.</p>
  105. *
  106. * Example:
  107. *
  108. * <pre><code>// executes after 1000ms the callback
  109. * delayed.delay(1000);</code></pre>
  110. *
  111. * @method delay
  112. * @param {Number} delay Delay in milliseconds.
  113. * @param {function} newFn Callback.
  114. * @param {Object} newScope Context object. Optional.
  115. * @param newArgs 0..n additional arguments that should be provided to the listener.
  116. */
  117. delay: function(delay, newFn, newScope, newArgs) {
  118. var instance = this;
  119. if (instance._id && instance._delay != delay) {
  120. instance.cancel();
  121. }
  122. instance._delay = delay || instance._delay;
  123. instance._time = instance._getTime();
  124. instance._fn = newFn || instance._fn;
  125. instance._scope = newScope || instance._scope;
  126. instance._args = newArgs || instance._args;
  127. if (!A.Lang.isArray(instance._args)) {
  128. instance._args = [instance._args];
  129. }
  130. if (!instance._id) {
  131. if (instance._delay > 0) {
  132. instance._id = setInterval(instance._base, instance._delay);
  133. }
  134. else {
  135. instance._base();
  136. }
  137. }
  138. },
  139. /**
  140. * Cancel the delayed task in case it's running (i.e., clearInterval from
  141. * the current running <a href="DelayedTask.html#property__id">_id</a>).
  142. *
  143. * @method cancel
  144. */
  145. cancel: function() {
  146. var instance = this;
  147. if (instance._id) {
  148. clearInterval(instance._id);
  149. instance._id = null;
  150. }
  151. },
  152. /**
  153. * Get the current timestamp (i.e., now).
  154. *
  155. * @method _getTime
  156. * @protected
  157. * @return {Number} Current timestamp
  158. */
  159. _getTime: function() {
  160. var instance = this;
  161. return (+new Date());
  162. }
  163. };
  164. A.DelayedTask = DelayedTask;