Path: blob/trunk/third_party/closure/goog/async/delay.js
4062 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Defines a class useful for handling functions that must be8* invoked after a delay, especially when that delay is frequently restarted.9* Examples include delaying before displaying a tooltip, menu hysteresis,10* idle timers, etc.11* @see ../demos/timers.html12*/131415goog.provide('goog.async.Delay');1617goog.require('goog.Disposable');18goog.require('goog.Timer');19202122/**23* A Delay object invokes the associated function after a specified delay. The24* interval duration can be specified once in the constructor, or can be defined25* each time the delay is started. Calling start on an active delay will reset26* the timer.27*28* @param {function(this:THIS)} listener Function to call when the29* delay completes.30* @param {number=} opt_interval The default length of the invocation delay (in31* milliseconds).32* @param {THIS=} opt_handler The object scope to invoke the function in.33* @template THIS34* @constructor35* @struct36* @extends {goog.Disposable}37* @final38*/39goog.async.Delay = function(listener, opt_interval, opt_handler) {40'use strict';41goog.async.Delay.base(this, 'constructor');4243/**44* The function that will be invoked after a delay.45* @private {function(this:THIS)}46*/47this.listener_ = listener;4849/**50* The default amount of time to delay before invoking the callback.51* @type {number}52* @private53*/54this.interval_ = opt_interval || 0;5556/**57* The object context to invoke the callback in.58* @type {Object|undefined}59* @private60*/61this.handler_ = opt_handler;626364/**65* Cached callback function invoked when the delay finishes.66* @type {Function}67* @private68*/69this.callback_ = goog.bind(this.doAction_, this);70};71goog.inherits(goog.async.Delay, goog.Disposable);727374/**75* Identifier of the active delay timeout, or 0 when inactive.76* @type {number}77* @private78*/79goog.async.Delay.prototype.id_ = 0;808182/**83* Disposes of the object, cancelling the timeout if it is still outstanding and84* removing all object references.85* @override86* @protected87*/88goog.async.Delay.prototype.disposeInternal = function() {89'use strict';90goog.async.Delay.base(this, 'disposeInternal');91this.stop();92delete this.listener_;93delete this.handler_;94};959697/**98* Starts the delay timer. The provided listener function will be called after99* the specified interval. Calling start on an active timer will reset the100* delay interval.101* @param {number=} opt_interval If specified, overrides the object's default102* interval with this one (in milliseconds).103*/104goog.async.Delay.prototype.start = function(opt_interval) {105'use strict';106this.stop();107this.id_ = goog.Timer.callOnce(108this.callback_,109opt_interval !== undefined ? opt_interval : this.interval_);110};111112113/**114* Starts the delay timer if it's not already active.115* @param {number=} opt_interval If specified and the timer is not already116* active, overrides the object's default interval with this one (in117* milliseconds).118*/119goog.async.Delay.prototype.startIfNotActive = function(opt_interval) {120'use strict';121if (!this.isActive()) {122this.start(opt_interval);123}124};125126127/**128* Stops the delay timer if it is active. No action is taken if the timer is not129* in use.130*/131goog.async.Delay.prototype.stop = function() {132'use strict';133if (this.isActive()) {134goog.Timer.clear(this.id_);135}136this.id_ = 0;137};138139140/**141* Fires delay's action even if timer has already gone off or has not been142* started yet; guarantees action firing. Stops the delay timer.143*/144goog.async.Delay.prototype.fire = function() {145'use strict';146this.stop();147this.doAction_();148};149150151/**152* Fires delay's action only if timer is currently active. Stops the delay153* timer.154*/155goog.async.Delay.prototype.fireIfActive = function() {156'use strict';157if (this.isActive()) {158this.fire();159}160};161162163/**164* @return {boolean} True if the delay is currently active, false otherwise.165*/166goog.async.Delay.prototype.isActive = function() {167'use strict';168return this.id_ != 0;169};170171172/**173* Invokes the callback function after the delay successfully completes.174* @private175*/176goog.async.Delay.prototype.doAction_ = function() {177'use strict';178this.id_ = 0;179if (this.listener_) {180this.listener_.call(this.handler_);181}182};183184185