javascript - millisecond countdown/timer seems to pause on zero -
making simple millisecond countdown/timer, here live example using jsfiddle
my issue number 0 displays longer other numbers.
i fluid countdown, suggestions?
js
// how many seconds added counter countdown date.prototype.addseconds= function(s) { this.setseconds(this.getseconds()+s); return this; } // default 60 seconds var end = new date().addseconds(60); // change value seconds wanted count down var _second = 1000; var _minute = _second * 60; var timer; function getdigit(position, number) { numberstring = number + ""; return numberstring.substr (position + 1, 1); } function showremaining() { var countdownelement = document.getelementbyid('timer'); var = new date(); var distance = end - now; var minutes = math.floor( (distance % _minute * 60) / _minute ); var seconds = math.floor( (distance % _minute) / _second ); var milliseconds = distance % _second; var millisecond = getdigit(1, milliseconds); if (millisecond <= 0) { millisecond = 0; } countdownelement.innerhtml = seconds + 's ' + millisecond + 'ms'; //countdownelement.innerhtml = seconds + '.' + milliseconds; if (milliseconds < 0) { countdownelement.innerhtml = 'finished'; clearinterval(timer); } } timer = setinterval(showremaining, 10);
if needed
html
<div id="timer">a</div>
css
#timer { display:inline-block; padding:3px 5px; border:1px solid #666; font-family:tahoma; color:#999; font-size:12px; }
it looks if take string conversion out, pause disappears:
var milliseconds = distance % (_second / 100);
instead of
var millisecond = getdigit(1, milliseconds);
Comments
Post a Comment