Debian Codegraph @

closure-library/closure/goog/demos/timers.html

raw
1 <!DOCTYPE html>
2 <html>
3 <!--
4 Copyright 2010 The Closure Library Authors. All Rights Reserved.
5
6 Use of this source code is governed by the Apache License, Version 2.0.
7 See the COPYING file for details.
8 -->
9 <head>
10 <title>goog.Timer, goog.async.Throttle goog.async.Delay</title>
11 <script src="../base.js"></script>
12
13 <script>
14 goog.require('goog.async.Delay');
15 goog.require('goog.async.Throttle');
16 goog.require('goog.Timer');
17 goog.require('goog.dom');
18 </script>
19
20 <link rel="stylesheet" href="css/demo.css">
21 <style>
22 .userStatus {
23 font-style: italic;
24 font-weight: bold;
25 }
26 </style>
27 </head>
28 <body>
29 <h1>A Collection of Time Based Utilities</h1>
30 <h2>goog.async.Delay</h2>
31 <p>An action can be invoked after some delay.</p>
32 Delay (seconds): <input size="2" type="text" id="delaySeconds" value="2">
33 <button onclick="doDelay()">Start Delay</button>
34 <button onclick="doReset()">Restart Delay</button>
35 <br />
36 Delay Status: <span class="userStatus" id="delayStatus">Not Set</span>
37
38 <h2>goog.async.Throttle</h2>
39 A throttle prevents the action from being called more than once per time
40 interval.
41 <br />
42 'Create' the Throttle, then hit the 'Do Throttle' button a lot
43 of times. Notice the number of 'Hits' increasing with each button press.
44 <br />
45 The action will be invoked no more than once per time interval.
46 <p>
47 Throttle interval (seconds):
48 <input size="2" type="text" id="throttleSeconds" value="2">
49 <button onclick="doThrottleStart()">Create Throttle</button>
50 <button onclick="doThrottle()">Do Throttle</button>
51 <br />
52 Throttle Hits: <span class="userStatus" id="throttleHits"></span>
53 <br />
54 Throttle Action Called: <span class="userStatus" id="throttleStatus"></span>
55 </p>
56 <h2>goog.Timer</h2>
57 A timer can be set up to call a timeout function on every 'tick' of the timer.
58 <p>
59 Timer interval (seconds):
60 <input size="2" type="text" id="timerSeconds" value="1">
61 <button onclick="doTimerStart()">Start Timer</button>
62 <button onclick="doTimerStop()">Stop Timer</button>
63 <button onclick="doTimerRestart()">Restart Timer</button>
64 <br />
65 Timer Status: <span class="userStatus" id="timerStatus">Not Set</span>
66 </p>
67 <h2>goog.Timer.callOnce</h2>
68 Timer also has a useful utility function that can call an action after some
69 timeout.
70 <br />
71 This a shortcut/replacement for window.setTimeout, and has a
72 corresponding goog.Timer.clear as well, which stops the action.
73 <p>
74 Timeout (seconds): <input size="2" type="text" id="doOnceSeconds" value="2">
75 <button onclick="doOnce()">Go</button>
76 <button onclick="doOnceClear()">Clear</button>
77 <br />
78 Do Once Status: <span class="userStatus" id="doOnceStatus"></span>
79 </p>
80 <script>
81
82 /**
83 * Get the seconds from a document element.
84 * @param {string} id The id of the element
85 * @return {number}
86 */
87 var getSeconds = function (id) {
88 var time = Number(goog.dom.getElement(id).value);
89 if (isNaN(time)) {
90 alert('Please enter a Number');
91 return null;
92 } else {
93 return time;
94 }
95 };
96
97 /**
98 * Convert seconds to ms.
99 * @param {number} seconds The time in Seconds
100 * @return {number}
101 */
102 var inMs = function (seconds) {
103 return seconds * 1000;
104 };
105
106 /**
107 * Delay.
108 */
109
110 var delay = null;
111 var delayStatus = goog.dom.getElement('delayStatus');
112
113 /**
114 * Start the delay, on the button press.
115 */
116 var doDelay = function() {
117 if (delay) {
118 goog.dom.setTextContent(delayStatus, 'Delay already set.');
119 return;
120 }
121
122 var seconds = getSeconds('delaySeconds');
123 if (!goog.isNumber(seconds)) {
124 return;
125 }
126 delay = new goog.async.Delay(delayedAction, inMs(seconds));
127 delay.start();
128 goog.dom.setTextContent(delayStatus,
129 'Delay for: ' + seconds + ' seconds.');
130 };
131
132 /**
133 * Reset the delay.
134 */
135 var doReset = function(){
136 if (!delay) {
137 return;
138 }
139 goog.dom.setTextContent(delayStatus, 'Delay Restarted.');
140 delay.start();
141 };
142
143 /**
144 * Callback, after some delay.
145 */
146 var delayedAction = function() {
147 goog.dom.setTextContent(delayStatus, 'Action called.');
148 delay.dispose();
149 delay = null;
150 };
151
152 /**
153 * Throttle.
154 */
155
156 var throttle = null;
157 var throttleCount = 0;
158 var throttleFireCount = 0;
159 var throttleHits = goog.dom.getElement('throttleHits');
160 var throttleStatus = goog.dom.getElement('throttleStatus');
161
162 /**
163 * Start a Throttle.
164 */
165 var doThrottleStart = function() {
166 var seconds = getSeconds('throttleSeconds');
167 if (!goog.isNumber(seconds)) {
168 return;
169 }
170 // Get rid of an old one, if it exists.
171 if (throttle) {
172 throttle.dispose();
173 throttleCount = 0;
174 throttleFireCount = 0;
175 }
176 // Create the throttle object for the given time.
177 throttle = new goog.async.Throttle(throttleAction, inMs(seconds));
178 // Reset the hits and the count.
179 goog.dom.setTextContent(throttleHits, throttleFireCount);
180 goog.dom.setTextContent(throttleStatus, throttleCount);
181 };
182
183 /**
184 * Do the throttle action, this can be called as often as desired.
185 */
186 var doThrottle = function(){
187 if (throttle) {
188 // Fire the throttle, this will only actually 'fire' no more than
189 // once per interval.
190 throttle.fire();
191 goog.dom.setTextContent(throttleHits, ++throttleFireCount);
192 }
193 };
194
195 /**
196 * Throttle Action Callback.
197 */
198 var throttleAction = function() {
199 goog.dom.setTextContent(throttleStatus, ++throttleCount);
200 };
201
202 /**
203 * Timer.
204 */
205
206 var timer = null;
207 var timerStatus = goog.dom.getElement('timerStatus');
208 var tickCount = 0;
209
210 /**
211 * Start a timer.
212 */
213 var doTimerStart = function() {
214 var seconds = getSeconds('timerSeconds');
215 if (!goog.isNumber(seconds)) {
216 return;
217 }
218 if (timer) {
219 timer.dispose();
220 tickCount = 0;
221 }
222 // A timer can be created with no callback object,
223 // listen for the TICK event.
224 timer = new goog.Timer(inMs(seconds));
225 timer.start();
226 goog.events.listen(timer, goog.Timer.TICK, tickAction);
227 };
228
229 /**
230 * Stop the Timer.
231 */
232 var doTimerStop = function() {
233 if (timer) {
234 timer.stop();
235 }
236 };
237
238 /**
239 * Reset the Timer.
240 */
241 var doTimerRestart = function() {
242 if (timer) {
243 timer.start();
244 }
245 };
246
247 /**
248 * Tick callback, called whenever the Timer sends a TICK event.
249 */
250 var tickAction = function() {
251 tickCount++;
252 goog.dom.setTextContent(timerStatus, 'Got tick: ' + tickCount);
253 };
254
255 var doOnceTimer = null;
256 var doOnceStatus = goog.dom.getElement('doOnceStatus');
257
258 /*
259 * Do some action once, optional delay. Can not be restarted, like Delay,
260 * only cleared.
261 */
262 var doOnce = function() {
263 if (doOnceTimer) {
264 // Timer already set, do not reset it.
265 return;
266 }
267 var seconds = getSeconds('doOnceSeconds');
268 if (!goog.isNumber(seconds)) {
269 return;
270 }
271 goog.dom.setTextContent(doOnceStatus, 'Will call action in ' + seconds +
272 ' seconds.');
273 doOnceTimer = goog.Timer.callOnce(function() {
274 goog.dom.setTextContent(doOnceStatus, 'Action called.');
275 doOnceTimer = null;
276 }, inMs(seconds));
277 };
278
279 /*
280 * Clear the doOnce, do not do the action.
281 */
282 var doOnceClear = function() {
283 goog.Timer.clear(doOnceTimer);
284 doOnceTimer = null;
285 goog.dom.setTextContent(doOnceStatus,
286 'Timer cleared, action not called.');
287 };
288
289 </script>
290 </body>
291 </html>