OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @fileoverview Provides a countdown-based timer. |
| 7 * @author juanlang@google.com (Juan Lang) |
| 8 */ |
| 9 'use strict'; |
| 10 |
| 11 /** |
| 12 * A countdown timer. |
| 13 * @interface |
| 14 */ |
| 15 function Countdown() {} |
| 16 |
| 17 /** |
| 18 * Sets a new timeout for this timer. |
| 19 * @param {number} timeoutMillis how long, in milliseconds, the countdown lasts. |
| 20 * @param {Function=} cb called back when the countdown expires. |
| 21 * @return {boolean} whether the timeout could be set. |
| 22 */ |
| 23 Countdown.prototype.setTimeout = function(timeoutMillis, cb) {}; |
| 24 |
| 25 /** Clears this timer's timeout. */ |
| 26 Countdown.prototype.clearTimeout = function() {}; |
| 27 |
| 28 /** |
| 29 * @return {number} how many milliseconds are remaining until the timer expires. |
| 30 */ |
| 31 Countdown.prototype.millisecondsUntilExpired = function() {}; |
| 32 |
| 33 /** @return {boolean} whether the timer has expired. */ |
| 34 Countdown.prototype.expired = function() {}; |
| 35 |
| 36 /** |
| 37 * Constructs a new clone of this timer, while overriding its callback. |
| 38 * @param {Function=} cb callback for new timer. |
| 39 * @return {!Countdown} new clone. |
| 40 */ |
| 41 Countdown.prototype.clone = function(cb) {}; |
| 42 |
| 43 /** |
| 44 * Constructs a new timer. The timer has a very limited resolution, and does |
| 45 * not attempt to be millisecond accurate. Its intended use is as a |
| 46 * low-precision timer that pauses while debugging. |
| 47 * @param {number=} timeoutMillis how long, in milliseconds, the countdown |
| 48 * lasts. |
| 49 * @param {Function=} cb called back when the countdown expires. |
| 50 * @constructor |
| 51 * @implements {Countdown} |
| 52 */ |
| 53 function CountdownTimer(timeoutMillis, cb) { |
| 54 this.remainingMillis = 0; |
| 55 this.setTimeout(timeoutMillis || 0, cb); |
| 56 } |
| 57 |
| 58 CountdownTimer.TIMER_INTERVAL_MILLIS = 200; |
| 59 |
| 60 /** |
| 61 * Sets a new timeout for this timer. Only possible if the timer is not |
| 62 * currently active. |
| 63 * @param {number} timeoutMillis how long, in milliseconds, the countdown lasts. |
| 64 * @param {Function=} cb called back when the countdown expires. |
| 65 * @return {boolean} whether the timeout could be set. |
| 66 */ |
| 67 CountdownTimer.prototype.setTimeout = function(timeoutMillis, cb) { |
| 68 if (this.timeoutId) |
| 69 return false; |
| 70 if (!timeoutMillis || timeoutMillis < 0) |
| 71 return false; |
| 72 this.remainingMillis = timeoutMillis; |
| 73 this.cb = cb; |
| 74 if (this.remainingMillis > CountdownTimer.TIMER_INTERVAL_MILLIS) { |
| 75 this.timeoutId = |
| 76 window.setInterval(this.timerTick.bind(this), |
| 77 CountdownTimer.TIMER_INTERVAL_MILLIS); |
| 78 } else { |
| 79 // Set a one-shot timer for the last interval. |
| 80 this.timeoutId = |
| 81 window.setTimeout(this.timerTick.bind(this), this.remainingMillis); |
| 82 } |
| 83 return true; |
| 84 }; |
| 85 |
| 86 /** Clears this timer's timeout. */ |
| 87 CountdownTimer.prototype.clearTimeout = function() { |
| 88 if (this.timeoutId) { |
| 89 window.clearTimeout(this.timeoutId); |
| 90 this.timeoutId = undefined; |
| 91 } |
| 92 }; |
| 93 |
| 94 /** |
| 95 * @return {number} how many milliseconds are remaining until the timer expires. |
| 96 */ |
| 97 CountdownTimer.prototype.millisecondsUntilExpired = function() { |
| 98 return this.remainingMillis > 0 ? this.remainingMillis : 0; |
| 99 }; |
| 100 |
| 101 /** @return {boolean} whether the timer has expired. */ |
| 102 CountdownTimer.prototype.expired = function() { |
| 103 return this.remainingMillis <= 0; |
| 104 }; |
| 105 |
| 106 /** |
| 107 * Constructs a new clone of this timer, while overriding its callback. |
| 108 * @param {Function=} cb callback for new timer. |
| 109 * @return {!Countdown} new clone. |
| 110 */ |
| 111 CountdownTimer.prototype.clone = function(cb) { |
| 112 return new CountdownTimer(this.remainingMillis, cb); |
| 113 }; |
| 114 |
| 115 /** Timer callback. */ |
| 116 CountdownTimer.prototype.timerTick = function() { |
| 117 this.remainingMillis -= CountdownTimer.TIMER_INTERVAL_MILLIS; |
| 118 if (this.expired()) { |
| 119 window.clearTimeout(this.timeoutId); |
| 120 this.timeoutId = undefined; |
| 121 if (this.cb) { |
| 122 this.cb(); |
| 123 } |
| 124 } |
| 125 }; |
OLD | NEW |