0.0.1 • Published 9 years ago
tmr v0.0.1
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
dfull days0-999ddfull days with padding00-999dddfull days with padding000-999hfull hours of the day0-23hhfull hours of the day with padding00-23hhhfull hours total0-999mfull minutes of the hour0-59mmfull minutes of the hour with padding00-59mmmfull minutes total0-999sfull seconds of the minute0-59ssfull seconds of the minute with padding00-59sssfull seconds total0-999ztenths of a second0-9zzhundredths of a second00-99zzzmilliseconds000-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);