node package manager
We need your input. Help make JavaScript better: Take the 2017 JavaScript Ecosystem survey »

simple-stopwatch

simple-stopwatch

install

Load the script from the browser:

<script type="text/javascript" src="simple-stopwatch/index.js"></script>

Or install using npm:

$ npm install simple-stopwatch

usage

Use from the window:

simpleStopwatch().start();

Or use a CommonJS module loader and namespace it:

var stopwatch = require('simple-stopwatch');
 
stopwatch().start();

Start a basic count:

simpleStopwatch().on('tick', function (time) {
    
    console.log(time);
 
}).start();

Start a count and stop after 10 seconds:

simpleStopwatch(10000).on('tick', function (time) {
    
    console.log(time);
 
}).on('dong', function (time) {
    
    console.log(time, 'dong!');
 
}).start();

Format the time as seconds:milliseconds:

simpleStopwatch(10000, '%s:%ms').on('tick', function (time) {
    
    console.log(time);
 
}).on('dong', function (time) {
    
    console.log(time, 'dong!');
 
}).start();

Format the time as a countdown to the duration:

simpleStopwatch(10000, '%ds:%dms').on('tick', function (time) {
    
    console.log(time);
 
}).on('dong', function (time) {
    
    console.log(time, 'dong!');
 
}).start();

formats

For formatting times, use printf-style strings:

%h /* hours */
%m /* minutes */
%s /* seconds */
%ms /* milliseconds */
 
%dh /* hours until duration */
%dm /* minutes until duration */
%ds /* seconds until duration */
%dms /* milliseconds until duration */

Note that, in order to use countdown formatting, you need to have set a duration for the stopwatch.