javascript - Adding milliseconds to timer in html -
is there way add together milliseconds timer?
i'm using timer counting in seconds.
var count=99; var counter=setinterval(timer, 1000); //1000 run every 1 sec function timer(){ count=count-1; if (count <= 0){ clearinterval(counter); return; } document.getelementbyid("timer").innerhtml=count + " seconds"; // watch spelling }
thanks
timers won't run millisecond accuracy - not below standard js minimum timeout, timer events can queue , sorts of other issues. in event (no pun intended) can't see timer count 1000 times second.
just utilize window.requestanimationframe
instead , show difference between time passed function , reference start time of timer.
class="snippet-code-js lang-js prettyprint-override">var timer = document.getelementbyid('timer'); var expires = +new date() + 10000; (function update() { var = +new date(); var togo = expires - now; if (togo > 0) { timer.innerhtml = togo; window.requestanimationframe(update); } else { timer.innerhtml = 0; } })();
class="snippet-code-html lang-html prettyprint-override"><div id="timer"></div>
javascript html
No comments:
Post a Comment