luptimer

lightweight ultra precise (javascript) timer

How to use

First create an instance of luptimer

var lup = $luptimer();

Start, pause, reset the timer

//Resume from paused or start from 0
lup.start();
//Pause at current time
lup.pause();
//Reset to 0
lup.reset();

Display the time

//Syntax: .display('selector' [, 'unit']). If no unit of time is specified the function returns the time since start() as a float.
//Note: resetting the timer will stop updating the element's text content, so 0 values need to be set manually on reset.

lup.display('#hours-dsp', 'hours');
lup.display('#mins-dsp', 'minutes');
lup.display('#secs-dsp', 'seconds');
lup.display('#millis-dsp', 'milliseconds');
lup.display('#micros-dsp', 'microseconds');

Get the time

Returns the total time since the page loaded, as a float
var t = lup.total();

Returns the time since start() as an integer, as the specified unit of time
//Syntax: .time(['unit']). If no unit of time is specified it defaults to milliseconds.
t = lup.time('hours');
t = lup.time('minutes');
t = lup.time('seconds');
t = lup.time('milliseconds');
t = lup.time('microseconds');

Browser Support

Luptimer uses the High Resolution Time API to provide microsecond time measurement capabilities, as well as the Animation Timing API for updating the time accurately and with little performance overhead. On browsers without an implementation of either APIs it falls back to more widely supported methods and to millisecond resolution.

Browser support for microsecond resolution:

U.S.A.85.24%
Global84.57%

Chrome20
Firefox15
IE10
Safari8
Opera15
Opera MiniNone
Android Browser4.4
Blackberry10
Chrome, Firefox, Opera and Safari for mobile have the same support as their desktop versions.

Note that even in supported browsers, accuracy varies by implementation. For example, Chrome 20 to 37 suffer from a bad implementation of the High Resolution Time API and only provide millisecond precision with a ~2ms deviation. See Chromium issue #158234.

Demo