Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/async/delay.js
4062 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Defines a class useful for handling functions that must be
9
* invoked after a delay, especially when that delay is frequently restarted.
10
* Examples include delaying before displaying a tooltip, menu hysteresis,
11
* idle timers, etc.
12
* @see ../demos/timers.html
13
*/
14
15
16
goog.provide('goog.async.Delay');
17
18
goog.require('goog.Disposable');
19
goog.require('goog.Timer');
20
21
22
23
/**
24
* A Delay object invokes the associated function after a specified delay. The
25
* interval duration can be specified once in the constructor, or can be defined
26
* each time the delay is started. Calling start on an active delay will reset
27
* the timer.
28
*
29
* @param {function(this:THIS)} listener Function to call when the
30
* delay completes.
31
* @param {number=} opt_interval The default length of the invocation delay (in
32
* milliseconds).
33
* @param {THIS=} opt_handler The object scope to invoke the function in.
34
* @template THIS
35
* @constructor
36
* @struct
37
* @extends {goog.Disposable}
38
* @final
39
*/
40
goog.async.Delay = function(listener, opt_interval, opt_handler) {
41
'use strict';
42
goog.async.Delay.base(this, 'constructor');
43
44
/**
45
* The function that will be invoked after a delay.
46
* @private {function(this:THIS)}
47
*/
48
this.listener_ = listener;
49
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
/**
58
* The object context to invoke the callback in.
59
* @type {Object|undefined}
60
* @private
61
*/
62
this.handler_ = opt_handler;
63
64
65
/**
66
* Cached callback function invoked when the delay finishes.
67
* @type {Function}
68
* @private
69
*/
70
this.callback_ = goog.bind(this.doAction_, this);
71
};
72
goog.inherits(goog.async.Delay, goog.Disposable);
73
74
75
/**
76
* Identifier of the active delay timeout, or 0 when inactive.
77
* @type {number}
78
* @private
79
*/
80
goog.async.Delay.prototype.id_ = 0;
81
82
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
'use strict';
91
goog.async.Delay.base(this, 'disposeInternal');
92
this.stop();
93
delete this.listener_;
94
delete this.handler_;
95
};
96
97
98
/**
99
* Starts the delay timer. The provided listener function will be called after
100
* the specified interval. Calling start on an active timer will reset the
101
* delay interval.
102
* @param {number=} opt_interval If specified, overrides the object's default
103
* interval with this one (in milliseconds).
104
*/
105
goog.async.Delay.prototype.start = function(opt_interval) {
106
'use strict';
107
this.stop();
108
this.id_ = goog.Timer.callOnce(
109
this.callback_,
110
opt_interval !== undefined ? opt_interval : this.interval_);
111
};
112
113
114
/**
115
* Starts the delay timer if it's not already active.
116
* @param {number=} opt_interval If specified and the timer is not already
117
* active, overrides the object's default interval with this one (in
118
* milliseconds).
119
*/
120
goog.async.Delay.prototype.startIfNotActive = function(opt_interval) {
121
'use strict';
122
if (!this.isActive()) {
123
this.start(opt_interval);
124
}
125
};
126
127
128
/**
129
* Stops the delay timer if it is active. No action is taken if the timer is not
130
* in use.
131
*/
132
goog.async.Delay.prototype.stop = function() {
133
'use strict';
134
if (this.isActive()) {
135
goog.Timer.clear(this.id_);
136
}
137
this.id_ = 0;
138
};
139
140
141
/**
142
* Fires delay's action even if timer has already gone off or has not been
143
* started yet; guarantees action firing. Stops the delay timer.
144
*/
145
goog.async.Delay.prototype.fire = function() {
146
'use strict';
147
this.stop();
148
this.doAction_();
149
};
150
151
152
/**
153
* Fires delay's action only if timer is currently active. Stops the delay
154
* timer.
155
*/
156
goog.async.Delay.prototype.fireIfActive = function() {
157
'use strict';
158
if (this.isActive()) {
159
this.fire();
160
}
161
};
162
163
164
/**
165
* @return {boolean} True if the delay is currently active, false otherwise.
166
*/
167
goog.async.Delay.prototype.isActive = function() {
168
'use strict';
169
return this.id_ != 0;
170
};
171
172
173
/**
174
* Invokes the callback function after the delay successfully completes.
175
* @private
176
*/
177
goog.async.Delay.prototype.doAction_ = function() {
178
'use strict';
179
this.id_ = 0;
180
if (this.listener_) {
181
this.listener_.call(this.handler_);
182
}
183
};
184
185