delay.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright 2007 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Defines a class useful for handling functions that must be
  16. * invoked after a delay, especially when that delay is frequently restarted.
  17. * Examples include delaying before displaying a tooltip, menu hysteresis,
  18. * idle timers, etc.
  19. * @author brenneman@google.com (Shawn Brenneman)
  20. * @see ../demos/timers.html
  21. */
  22. goog.provide('goog.Delay');
  23. goog.provide('goog.async.Delay');
  24. goog.require('goog.Disposable');
  25. goog.require('goog.Timer');
  26. /**
  27. * A Delay object invokes the associated function after a specified delay. The
  28. * interval duration can be specified once in the constructor, or can be defined
  29. * each time the delay is started. Calling start on an active delay will reset
  30. * the timer.
  31. *
  32. * @param {function(this:THIS)} listener Function to call when the
  33. * delay completes.
  34. * @param {number=} opt_interval The default length of the invocation delay (in
  35. * milliseconds).
  36. * @param {THIS=} opt_handler The object scope to invoke the function in.
  37. * @template THIS
  38. * @constructor
  39. * @struct
  40. * @extends {goog.Disposable}
  41. * @final
  42. */
  43. goog.async.Delay = function(listener, opt_interval, opt_handler) {
  44. goog.async.Delay.base(this, 'constructor');
  45. /**
  46. * The function that will be invoked after a delay.
  47. * @private {function(this:THIS)}
  48. */
  49. this.listener_ = listener;
  50. /**
  51. * The default amount of time to delay before invoking the callback.
  52. * @type {number}
  53. * @private
  54. */
  55. this.interval_ = opt_interval || 0;
  56. /**
  57. * The object context to invoke the callback in.
  58. * @type {Object|undefined}
  59. * @private
  60. */
  61. this.handler_ = opt_handler;
  62. /**
  63. * Cached callback function invoked when the delay finishes.
  64. * @type {Function}
  65. * @private
  66. */
  67. this.callback_ = goog.bind(this.doAction_, this);
  68. };
  69. goog.inherits(goog.async.Delay, goog.Disposable);
  70. /**
  71. * A deprecated alias.
  72. * @deprecated Use goog.async.Delay instead.
  73. * @constructor
  74. * @final
  75. */
  76. goog.Delay = goog.async.Delay;
  77. /**
  78. * Identifier of the active delay timeout, or 0 when inactive.
  79. * @type {number}
  80. * @private
  81. */
  82. goog.async.Delay.prototype.id_ = 0;
  83. /**
  84. * Disposes of the object, cancelling the timeout if it is still outstanding and
  85. * removing all object references.
  86. * @override
  87. * @protected
  88. */
  89. goog.async.Delay.prototype.disposeInternal = function() {
  90. goog.async.Delay.base(this, 'disposeInternal');
  91. this.stop();
  92. delete this.listener_;
  93. delete this.handler_;
  94. };
  95. /**
  96. * Starts the delay timer. The provided listener function will be called after
  97. * the specified interval. Calling start on an active timer will reset the
  98. * delay interval.
  99. * @param {number=} opt_interval If specified, overrides the object's default
  100. * interval with this one (in milliseconds).
  101. */
  102. goog.async.Delay.prototype.start = function(opt_interval) {
  103. this.stop();
  104. this.id_ = goog.Timer.callOnce(
  105. this.callback_, goog.isDef(opt_interval) ? opt_interval : this.interval_);
  106. };
  107. /**
  108. * Starts the delay timer if it's not already active.
  109. * @param {number=} opt_interval If specified and the timer is not already
  110. * active, overrides the object's default interval with this one (in
  111. * milliseconds).
  112. */
  113. goog.async.Delay.prototype.startIfNotActive = function(opt_interval) {
  114. if (!this.isActive()) {
  115. this.start(opt_interval);
  116. }
  117. };
  118. /**
  119. * Stops the delay timer if it is active. No action is taken if the timer is not
  120. * in use.
  121. */
  122. goog.async.Delay.prototype.stop = function() {
  123. if (this.isActive()) {
  124. goog.Timer.clear(this.id_);
  125. }
  126. this.id_ = 0;
  127. };
  128. /**
  129. * Fires delay's action even if timer has already gone off or has not been
  130. * started yet; guarantees action firing. Stops the delay timer.
  131. */
  132. goog.async.Delay.prototype.fire = function() {
  133. this.stop();
  134. this.doAction_();
  135. };
  136. /**
  137. * Fires delay's action only if timer is currently active. Stops the delay
  138. * timer.
  139. */
  140. goog.async.Delay.prototype.fireIfActive = function() {
  141. if (this.isActive()) {
  142. this.fire();
  143. }
  144. };
  145. /**
  146. * @return {boolean} True if the delay is currently active, false otherwise.
  147. */
  148. goog.async.Delay.prototype.isActive = function() {
  149. return this.id_ != 0;
  150. };
  151. /**
  152. * Invokes the callback function after the delay successfully completes.
  153. * @private
  154. */
  155. goog.async.Delay.prototype.doAction_ = function() {
  156. this.id_ = 0;
  157. if (this.listener_) {
  158. this.listener_.call(this.handler_);
  159. }
  160. };