npm install --save horologe
horologe
is now at version 2. There have been changes. Check this documentation to see what is different from version 1.
var Timer = require('horologe');
var count = 0;
var timer = Timer.create(1000, {sync: true});
timer.on('tick', function(time, passed){
var d = new Date(time);
console.log((++count) + ' ' + d.getHours()+':'+d.getMinutes()+':'+d.getSeconds() + ' ' + passed);
});
timer.start(20);
or create, and setup all at once.
var count = 0, timer = Timer.create(1000, {sync: true}).on('tick', onTick).start(5);
function onTick(time, passed){
var d = new Date(time);
console.log((++count)+d.getHours()+':'+d.getMinutes()+':'+d.getSeconds() + ' ' + passed);
}
interval
is the time between ticks.
options
is an optional object argument.
There are two ways to create a timer.
let timer1 = Timer.create(1000, {});
let timer2 = new Timer(1000, {});
What should the timer be synchronized to? Using milliseconds you can adjust the timer synchronization to system time. Using a falsy value will set no synchronization.
Set a tick function. This is the same as timer.on('tick', ()=>{})
options.highres
is an experimental option. Setting it will make the timer use performance.now()
instead of Date.now()
for timing.
Set the name property of the timer.
The interval set in the constructor.
Is the timer paused?
Is the timer running?
How many ticks of the timer have been done?
What's the percentage of time complete until the timer is stopped by it's range? If the range is infinite timer.percent
returns zero.
The name set in the options.
Get the time that the timer started. timer.startTime
returns null
if the timer is not running.
Start the timer at the current time.
The wait
parameter is optional. Specify an integer duration for the first tick to wait. If wait isn't set then the most optimal period before the first tick is chosen using rounding to the nearest interval in the past, or future.
If the timer has already started, and hasn't been paused timer.start()
does nothing.
timer.range(amount)
sets the time range, or when the timer ends. The default is Infinity
.
Stop the timer. timer.stop()
resets the timer entirely to a non-running, and non-paused state.
Pause the timer.
limit
has a default of Infinity
.
limit
controls how long the timer will be paused.
Set a listener on an event.
Remove a listener.
Destroy the timer.
Emitted at every interval of the timer.
The arguments to the listener are time
, and passed
.
timer.on('tick', function(time, passed){
});
time
is the current system time.
passed
is how much time has passed since the timer was started.
Emitted when the timer is started.
startTime
is the only argument for the start
listener.
timer.on('start', function(startTime){
});
Emitted when the timer is stopped. No arguments are passed to the listener.
Emitted when the timer is paused. No arguments are passed to the listener.
Emitted when the timer ends, and only if an amount is passed to the range
method.
timer.on('complete', (time, passed)=>{
});
The horologe
timer is very close to the millisecond
precision. At worst it will be off by a 100th of a second. In my tests it remained in the 2 millisecond range of precision for most iterations of the interval. horologe
doesn't use precision.now
so keep that in mind.