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 = 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 beatmetronome;// Start metronome. Emits a 'start' event.metronomestart;// practice your instrument...// Change the tempo, immediately. Emits of a 'set' event.metronome;// Stop metronome. Emits a 'stop' event.metronome;
var Countdown = 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.countdown;// Start the timer. Emits a 'start' event.countdown;countdownstart;// Pause the timer. Emits a 'stop' eventcountdown;countdown; // you could also call `#stop` for the same behavior.// Resume the timer from wherever you left offcountdown; // 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.countdown;// 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.countdown;// It emits a 'finish' event, when timer runs out.countdown;
var Stopwatch = Stopwatch;var stopwatch = ;// It emits a 'tick' event, once per second.stopwatch;// Start the stopwatch. Emits a 'start' event.stopwatchstart;// Split time, save to the `laps` array. Emits of 'split' event.// The stopwatch continues running.stopwatch;stopwatch;// Stop the stopwatch. Emits a 'stop' event.stopwatch;// Reset the timer back to 0 seconds. Does not start it again.stopwatch;