Dashboard sipadu mbip
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.

aui-delayed-task.js 4.1KB

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