Timer
Countdown timer that starts from a given initial time and counts down to zero. The countdown can be paused, resumed, and stopped.
Usage
ts
import { Timer, TimeSpan } from './';
const timer = new Timer(5000, {
interval: 1000,
onTick() {
console.log(`Time: ${time.toString()}`);
}
});
const time = new TimeSpan(() => timer.time);
time.setDefaultFormat("mm:ss");
Timer class
ts
class Timer
Timer that counts down a time value.
Constructors
ts
constructor(time: ValueOrGetter<number>, options: TimerOptions = {})
Creates a new Timer.
Parameters
time: ValueOrGetter<number>
The initial time for the timer.options: TimerOptions = {}
The options for the timer.
Properties
Name | Type | Description |
---|---|---|
initTime | number | get Get the initial time value. |
time | number | get Get the countdown time value. |
state | TimerState | get Get the current state of the timer. |
start method
ts
start(): void
Starts the timer.
pause method
ts
pause(): void
Pauses the timer.
resume method
ts
resume(): void
Resumes the timer.
stop method
ts
stop(): void
Stops the timer.
TimerState type
ts
type TimerState = ElapseState | 'finished'
The state of the Timer instance.
TimerOptions interface
ts
interface TimerOptions extends ElapseOptions
Options for the Timer class.
Properties
Name | Type | Description |
---|---|---|
stopOnFinish | boolean | optional Stops the interval when the countdown finishes. |
interval | number | IntervalOptions | optional The interval or options for the interval. |
timestamp | () => number | optional Function that returns the current timestamp. |
onTick | (time: number) => void | optional Function to be called on each tick. |
driftAdjust | boolean | optional Adjust any drift in the elapsed time to the nearest multiple of the interval delay value if exist. |