jstimer
v1.0.14
Published
JavaScript timer manager
Downloads
31
Maintainers
Readme
An optimized JavaScript timer
JavaScript timer that in the background have just one native timer (setInterval)
Install
npm i jstimer -S
or
yarn add jstimer -S
Test
npm test
Use
Start one time timer (equal to setTimeout
)
import {Timer} from 'jstimer';
/* Will tick each 2 seconds, 4 times. */
let timer = new Timer(2000, 4);
timer.on('tick', () => {
console.log('timer tick');
});
timer.on('done', () => {
console.log('timer done');
});
timer.start();
Start infinite timer (equal to setInterval
)
import {Timer} from 'jstimer';
/* TImer will tick each 1 second, forever. */
let timer = new Timer(1000);
timer.on('tick', () => {
console.log('timer tick');
});
timer.start();
Stop a timer
import {Timer} from 'jstimer';
let timer = new Timer(1000);
timer.on('tick', () => {
console.log('timer has ticked');
});
timer.on('stopped', () => {
console.log(`timer has stopped`);
});
timer.start();
setImmediate(() => {
timer.stop();
});