Path: blob/trunk/third_party/closure/goog/async/delay.js
2868 views
// Copyright 2007 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Defines a class useful for handling functions that must be16* 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 [email protected] (Shawn Brenneman)20* @see ../demos/timers.html21*/222324goog.provide('goog.Delay');25goog.provide('goog.async.Delay');2627goog.require('goog.Disposable');28goog.require('goog.Timer');29303132/**33* A Delay object invokes the associated function after a specified delay. The34* interval duration can be specified once in the constructor, or can be defined35* each time the delay is started. Calling start on an active delay will reset36* the timer.37*38* @param {function(this:THIS)} listener Function to call when the39* delay completes.40* @param {number=} opt_interval The default length of the invocation delay (in41* milliseconds).42* @param {THIS=} opt_handler The object scope to invoke the function in.43* @template THIS44* @constructor45* @struct46* @extends {goog.Disposable}47* @final48*/49goog.async.Delay = function(listener, opt_interval, opt_handler) {50goog.async.Delay.base(this, 'constructor');5152/**53* The function that will be invoked after a delay.54* @private {function(this:THIS)}55*/56this.listener_ = listener;5758/**59* The default amount of time to delay before invoking the callback.60* @type {number}61* @private62*/63this.interval_ = opt_interval || 0;6465/**66* The object context to invoke the callback in.67* @type {Object|undefined}68* @private69*/70this.handler_ = opt_handler;717273/**74* Cached callback function invoked when the delay finishes.75* @type {Function}76* @private77*/78this.callback_ = goog.bind(this.doAction_, this);79};80goog.inherits(goog.async.Delay, goog.Disposable);81828384/**85* A deprecated alias.86* @deprecated Use goog.async.Delay instead.87* @constructor88* @final89*/90goog.Delay = goog.async.Delay;919293/**94* Identifier of the active delay timeout, or 0 when inactive.95* @type {number}96* @private97*/98goog.async.Delay.prototype.id_ = 0;99100101/**102* Disposes of the object, cancelling the timeout if it is still outstanding and103* removing all object references.104* @override105* @protected106*/107goog.async.Delay.prototype.disposeInternal = function() {108goog.async.Delay.base(this, 'disposeInternal');109this.stop();110delete this.listener_;111delete this.handler_;112};113114115/**116* Starts the delay timer. The provided listener function will be called after117* the specified interval. Calling start on an active timer will reset the118* delay interval.119* @param {number=} opt_interval If specified, overrides the object's default120* interval with this one (in milliseconds).121*/122goog.async.Delay.prototype.start = function(opt_interval) {123this.stop();124this.id_ = goog.Timer.callOnce(125this.callback_, goog.isDef(opt_interval) ? opt_interval : this.interval_);126};127128129/**130* Starts the delay timer if it's not already active.131* @param {number=} opt_interval If specified and the timer is not already132* active, overrides the object's default interval with this one (in133* milliseconds).134*/135goog.async.Delay.prototype.startIfNotActive = function(opt_interval) {136if (!this.isActive()) {137this.start(opt_interval);138}139};140141142/**143* Stops the delay timer if it is active. No action is taken if the timer is not144* in use.145*/146goog.async.Delay.prototype.stop = function() {147if (this.isActive()) {148goog.Timer.clear(this.id_);149}150this.id_ = 0;151};152153154/**155* Fires delay's action even if timer has already gone off or has not been156* started yet; guarantees action firing. Stops the delay timer.157*/158goog.async.Delay.prototype.fire = function() {159this.stop();160this.doAction_();161};162163164/**165* Fires delay's action only if timer is currently active. Stops the delay166* timer.167*/168goog.async.Delay.prototype.fireIfActive = function() {169if (this.isActive()) {170this.fire();171}172};173174175/**176* @return {boolean} True if the delay is currently active, false otherwise.177*/178goog.async.Delay.prototype.isActive = function() {179return this.id_ != 0;180};181182183/**184* Invokes the callback function after the delay successfully completes.185* @private186*/187goog.async.Delay.prototype.doAction_ = function() {188this.id_ = 0;189if (this.listener_) {190this.listener_.call(this.handler_);191}192};193194195