Skip to content

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 ​

NameTypeDescription
initTimenumberget Get the initial time value.
timenumberget Get the countdown time value.
stateTimerStateget 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 ​

NameTypeDescription
stopOnFinishbooleanoptional Stops the interval when the countdown finishes.
intervalnumber | IntervalOptionsoptional The interval or options for the interval.
timestamp() => numberoptional Function that returns the current timestamp.
onTick(time: number) => voidoptional Function to be called on each tick.
driftAdjustbooleanoptional Adjust any drift in the elapsed time to the nearest multiple of the interval delay value if exist.

MIT License, Made with ❤️