Speed counter made easy
npm i speed-beat
Start to track many resource and, every timer
second execute the chrono
function!
const { speedBeat } = require('speed-beat')
const speedTracker = speedBeat({ timer: '1s' })
// the chrono functions are executed every `timer` ms
speedTracker.chrono('john', doSomething)
speedTracker.chrono('bolt', doSomething)
speedTracker.chrono('heavy', doSomething)
// register the speed for each rider adding 1 unit by default
setInterval(() => { speedTracker.lap('john') }, 300)
setInterval(() => { speedTracker.lap('bolt') }, 50)
// you can pass a number value for faster rider
setInterval(() => { speedTracker.lap('heavy', 15) }, 600)
function doSomething (id, counter, total) {
console.log(`${id} speed is ${counter} and hit ${total} right now`)
}
You will handle when execute the chrono
function.
const { speedBeat } = require('speed-beat')
const speedTracker = speedBeat()
// the chrono functions are executed every `timer` ms
speedTracker.chrono('john', doOnce)
speedTracker.chrono('bolt', doOnce)
speedTracker.chrono('heavy', doOnce)
// register the speed for each rider adding 1 unit by default
setInterval(() => { speedTracker.lap('john') }, 300).unref()
setInterval(() => { speedTracker.lap('bolt') }, 50).unref()
// you can pass a number value for faster rider
setInterval(() => { speedTracker.lap('heavy', 15) }, 600).unref()
setTimeout(speedTracker.finish, 3000)
// you can call .finish many time as you want as well
// setInterval(speedTracker.finish, 3000)
function doOnce (id, counter, total, deltaBeat) {
console.log(`${id} speed is ${counter} and hit ${total} in ${deltaBeat} ms`)
}
speedBeat(options)
: create aspeedBeat
instance.chrono(id, function): EventEmitter
: add a speed element to track.lap(id[, value]): EventEmitter
: increment the speed value for the elementid
. If the id doesn't exist nothing happen.driver(id): EventEmitter
: return the speedid
element.timer()
: return thetimer
in millisec.finish()
: stop the call to the chrono functions
The chrono
function will have 4 parameters:
id
: the name of the speed elementcounter
: the summation of thelap
values since the last beat was triggertotal
: the total amount oflap
valuesdeltaBeat
: the time in millisec that has passed away since last beat
timer
: the beat of chrono functions. It can be expressed in milliseconds or as a string in the ms format
Copyright Manuel Spigolon, Licensed under MIT.