UNPKG

stopwatch-emitter

Version:
75 lines (54 loc) 1.81 kB
# 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 ```javascript // 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 ```javascript 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](https://github.com/Wolfy87/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. ```javascript 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. ```javascript var stopwatch = new Stopwatch('60s'); stopwatch.getCurrentTime(); // 0 stopwatch.getMaxTime(); // 60 // Start and wait 20 seconds... stopwatch.start() stopwatch.getRemainingTime(); // 40 stopwatch.isRunning(); // true ```