TM-Timer
A simple count-down Timer class, based on TM-Ticker (no GUI)
Installation
$ npm install tm-timer
;// orconst Timer = ;
TL;DR
Create:
// construct & configconst t = duration finalCallback // or just construct (and config later)const t =
Config:
ttt
Use:
now
is optional
tstartnow
t
t
t
t
API
Constructor
const t = duration finalCallback;
-
duration
[number, optional*]
* Required to start. optional at construction.
Countdown in milliseconds. Passnull
to set the callback alone. -
finalCallback
[function, optional]
The function you want to call when countdown is over.
Example:
const fiveMinutes = 5 * 60 * 1000; const t = fiveMinutes { console;};
Configuration
A Timer instance won't tick unless it has a duration.
You can set the timer's duration and final callback on construction or later with the following methods which are very self explanatory:
const myTimer = ; myTimermyTimermyTimer
.set(duration)
duration
[number, required]
Set the total time in milliseconds to count down to.
const fiveMinutes = 5 * 60 * 1000; myTimer;
.whenDone(finalCallback)
finalCallback
[function, required]
Runs when timer finishes. Gets no arguments.
myTimer;
.onTick(callback)
As the timer counts down, it ticks every 500ms (twice every second).
Use when you can bind a tick handler function.
The first tick happens on start, synchronously, before any timeout.
callback
[function, optional]
The callback function recieves two arguments:-
isBigTick
[boolean]
Equalstrue
on "big" ticks (a whole second: 0, 1000, 2000, 3000 etc.)
Equalsfalse
on "small" ticks (half a second: 500, 1500, 2500, 3500 etc.)
For example, you could update your clock's digits on big ticks and blink the clock's colons on small ticks (04:20). -
timeLeft
[number]
Time left in milliseconds.
-
Example:
myTimer;
Methods
All methods can get called with a timestamp
argument. Pass in a current timestamp when you need to sync time with other modules.
timestamp
(ms, number, optional) - The timestamp to be considered as the method's execution time.
.start()
Start counting down.
Calls the first tick (if a tick handler is set with .onTick(fn)
).
When called after a .stop()
it acts as a "resume" function. There will be no start-tick in this case.
// optionalconst timestamp = Date myTimerstarttimestamp
.getTimeLeft()
Returns how many milliseconds left.
const myTimer = 3000 callback myTimerstart // after 2480 ms (for example)myTimer // --> 520
.stop()
Stop/Pause counting down.
Run .start()
to resume.
const myTimer = fiveMinutes gameOver myTimerstart // Take a breakmyTimer // ResumemyTimerstart
.reset()
Reset the countdown with full original duration.
Can be called whether the timer is running or not. When called while running, it acts like a "restart" and doesn't stop the timer.
const myTimer = fiveMinutes gameOver myTimerstart /* after 2 minutes */myTimer // re-start counting down five minutes. No stop.
const myTimer = fiveMinutes gameOver myTimerstart /* after 2 minutes */myTimermyTimermyTimerstart // start counting down five minutes.
.destroy()
Destroy the timer. Removes duration and bound callbacks.
Cannot be used again unless re-configured.
const myTimer = fiveMinutes gameOver myTimerstartmyTimer myTimermyTimer
Playground
$ npm run playground