countdown timer, stopwatch and metronome
A set of event emitters, built around
setInterval. It's for casual use and
the API is pretty straight-forward. I would not recommend the stopwatch for
benchmarking, performance tests, or anything else requiring accuracy finer
The base class is
Timepiece. From that, there are a few subclasses.
Metronome: set the tempo, by beats per minute. Add your event listeners to the 'tick' event.
Countdown: count down timer, set from a number of seconds.
Stopwatch: measure elapsed time and save splits.
setIntervalfires events, don't use this if you need precision to-the-millisecond or finer.
All classes will these emit these events:
'start': emitted when the object starts.
'stop': emitted when the object stops.
'set': emitted when you change some setting on an object. The setting will vary from class to class.
'tick': once an object has started, it will emit a 'tick' event at some interval of time. The interval is once per second, unless of course, you are using a
Metronomeat a tempo other than the default.
$ npm install timepiece
var Metronome = require'timepiece'Metronome;// By default, a metronome object is set to 60 bpm.var metronome = ;// But you could also initialize one at another tempo.var allegro = 144;// It emits a 'tick' event on each beatmetronomeon'tick'console.logmetronomebpm + ' bpm';;// Start metronome. Emits a 'start' event.metronomestart;// practice your instrument...// Change the tempo, immediately. Emits of a 'set' event.metronomeset108;// Stop metronome. Emits a 'stop' event.metronomestop;
var Countdown = require'timepiece'Countdown;// By default, count down from 60 seconds.var countdown = ;// But you could also count down from any other span, set by number of seconds.var countdownFrom90 = 90; // a minute & thirty seconds// It emits a 'tick' event, once per second.countdownon'tick'console.logcountdownremaining;;// Start the timer. Emits a 'start' event.countdownon'start'console.log'starting timer at ' + countdownfrom + ' seconds';;countdownstart;// Pause the timer. Emits a 'stop' eventcountdownon'stop'console.log'stopped timer with' + countdownremaining + ' seconds left.';;countdownpause; // you could also call `#stop` for the same behavior.// Resume the timer from wherever you left offcountdownresume; // you could also call `#start`// Reset the timer to its original count down setting// NOTE: this does not start the timer again, you do this manually.countdownreset;// Set the timer to count down from another value// NOTE: this method checks if the timer is active, before doing anything.// If the timer is already active, then nothing happens.countdownset30;// It emits a 'finish' event, when timer runs out.countdownon'finish'console.log'exciting stuff!';;
var Stopwatch = require'timepiece'Stopwatch;var stopwatch = ;// It emits a 'tick' event, once per second.stopwatchon'tick'console.logstopwatchelapsed + ' seconds';;// Start the stopwatch. Emits a 'start' event.stopwatchstart;// Split time, save to the `laps` array. Emits of 'split' event.// The stopwatch continues running.stopwatchon'split'console.logstopwatchlaps;;stopwatchsplit;// Stop the stopwatch. Emits a 'stop' event.stopwatchstop;// Reset the timer back to 0 seconds. Does not start it again.stopwatchreset;