stopwatch-emitter

0.0.4 • Public • Published

Stopwatch

Stopwatch is a simple stopwatch-like timer that can emit events. It currently works only in seconds and will parse a string for seconds, minutes, and hours, and fractions thereof.

Example usage

Creating instances

// Node
var Stopwatch = require('stopwatch-emitter').Stopwatch;
var stopwatch1 = new Stopwatch('5m');
var stopwatch2 = new Stopwatch('30s');

// Browser
var Stopwatch = new Stopwatch('0.5h');

Main methods

var stopwatch = new Stopwatch('5m');

// Start
stopwatch.start();

// Stop
stopwatch.stop();

// Pause
stopwatch.pause();

// Restart from any spot
stopwatch.restart();

Events

Stopwatch implements a classic event emitter. The node version uses the node event emitter and the browser version uses Oliver Caldwell's implementation of EventEmitter (thanks!). All events have the same name as the method that invokes them. An additional event tick is also available which, as you might imagine, is called every second the stopwatch ticks.

var stopwatch = new Stopwatch('5m');

stopwatch.on('start', function(){
  console.log('Started!');
});

stopwatch.on('pause', function(){
  console.log('Paused!');
});

stopwatch.start(); // Started!
stopwatch.pause(); // Paused!

stopwatch.on('tick', function(){
  console.log('Tick!');
});

stopwatch.restart();
// Tick!
// Tick!

Other methods

Stopwatch also have a few getters for time remaining, current time, and the max time, all in seconds. It also has an isRunning() call.

var stopwatch = new Stopwatch('60s');

stopwatch.getCurrentTime(); // 0
stopwatch.getMaxTime(); // 60

// Start and wait 20 seconds...
stopwatch.start()

stopwatch.getRemainingTime(); // 40
stopwatch.isRunning(); // true

Readme

Keywords

none

Package Sidebar

Install

npm i stopwatch-emitter

Weekly Downloads

0

Version

0.0.4

License

none

Last publish

Collaborators

  • bfricka