timers.html 7.7 KB

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