npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

timepiece

v0.4.0

Published

countdown timer, stopwatch and metronome

Downloads

3

Readme

timepiece

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 than to-the-centisecond.

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.
    • Because of the non-guarantee of when exactly setInterval fires 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 Metronome at a tempo other than the default.

Installation

$ npm install timepiece

Usage

Metronome

var Metronome = require('timepiece').Metronome;

// By default, a metronome object is set to 60 bpm.
var metronome = new Metronome();
// But you could also initialize one at another tempo.
var allegro = new Metronome(144);

// It emits a 'tick' event on each beat
metronome.on('tick', function logBPM(){
  console.log(metronome.bpm + ' bpm');
});

// Start metronome. Emits a 'start' event.
metronome.start();

// practice your instrument...

// Change the tempo, immediately. Emits of a 'set' event.
metronome.set(108);

// Stop metronome. Emits a 'stop' event.
metronome.stop();

Countdown

var Countdown = require('timepiece').Countdown;

// By default, count down from 60 seconds.
var countdown = new Countdown();
// But you could also count down from any other span, set by number of seconds.
var countdownFrom90 = new Countdown(90); // a minute & thirty seconds

// It emits a 'tick' event, once per second.
countdown.on('tick', function reportTimeLeft() {
  console.log(countdown.remaining);
});

// Start the timer. Emits a 'start' event.
countdown.on('start', function stopped() {
  console.log('starting timer at ' + countdown.from + ' seconds');
});
countdown.start();

// Pause the timer. Emits a 'stop' event
countdown.on('stop', function stopped() {
  console.log('stopped timer with' + countdown.remaining + ' seconds left.');
});
countdown.pause(); // you could also call `#stop` for the same behavior.

// Resume the timer from wherever you left off
countdown.resume(); // 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.reset();

// 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.set(30);

// It emits a 'finish' event, when timer runs out.
countdown.on('finish', function happy() {
  console.log('exciting stuff!');
});

Stopwatch

var Stopwatch = require('timepiece').Stopwatch;

var stopwatch = new Stopwatch();

// It emits a 'tick' event, once per second.
stopwatch.on('tick', function reportElapsedTime() {
  console.log(stopwatch.elapsed + ' seconds');
});

// Start the stopwatch. Emits a 'start' event.
stopwatch.start();

// Split time, save to the `laps` array. Emits of 'split' event.
// The stopwatch continues running.
stopwatch.on('split', function showLaps() {
  console.log(stopwatch.laps);
});
stopwatch.split();

// Stop the stopwatch. Emits a 'stop' event.
stopwatch.stop();

// Reset the timer back to 0 seconds. Does not start it again.
stopwatch.reset();