tmr
tmr
is a very simple countdown clock. all values are in ms.
new tmr([init])
create a new timer with an optonal initial value for countdown. if now initial value is passed or the initial value is zero, the clock will alway count up.
tmr.time()
get the elapsed time.
tmr.get()
get the offset from the initial value or, if no initial value is set, the elapsed time.
tmr.start()
start or restart the clock.
tmr.stop()
stop the clock.
tmr.reset([init])
reset the clock, optionally set a new initial value.
tmr.change(offset)
change the elapsed time by offset.
tmr.set(elapsed)
set the elapsed time.
tmr.check([val])
check if the elapsed time is lower than the initial value or val
tmr.format(fmt, up)
get a formatted string of the clock offset or elapsed time. setting up
true, will always give the elapsed time.
formatting
d
full days0-999
dd
full days with padding00-999
ddd
full days with padding000-999
h
full hours of the day0-23
hh
full hours of the day with padding00-23
hhh
full hours total0-999
m
full minutes of the hour0-59
mm
full minutes of the hour with padding00-59
mmm
full minutes total0-999
s
full seconds of the minute0-59
ss
full seconds of the minute with padding00-59
sss
full seconds total0-999
z
tenths of a second0-9
zz
hundredths of a second00-99
zzz
milliseconds000-999
example
var tmr = require("tmr");
var timer = new timer(60); // 60 seconds countdown
timer.start();
setInterval(function(){
if (!timer.check()) throw new Error("detonation");
if (blue_cable_cut) timer.set(5000);
if (red_cable_cut) timer.stop();
console.log("%s till detonation", timer.format("d:hh:mm:ss.zzz"));
console.log("counting down since %s seconds", timer.format("sss", true));
},1000);