javascript - Add "0" to countdown timer after it reaches 0 -
so basicly have countdown timer add "0" once reaches below "10". far have tried if statement (in code below) did not work. here full code:
countdowntimer('01/1/2017 12:0 am', 'countdown'); countdowntimer('01/1/2017 12:0 am', 'newcountdown'); function countdowntimer(dt, id) { var end = new date(dt); var _second = 1000; var _minute = _second * 60; var _hour = _minute * 60; var _day = _hour * 24; var timer; function showremaining() { var = new date(); var distance = end - now; if (distance < 0) { clearinterval(timer); document.getelementbyid(id).innerhtml = 'expired!'; return; } var days = math.floor(distance / _day); var hours = math.floor((distance % _day) / _hour); var minutes = math.floor((distance % _hour) / _minute); var seconds = math.floor((distance % _minute) / _second); // "if" statement did not work, else working if (seconds < 10) { document.getelementbyid(id).innerhtml += '0' + seconds + '.'; } document.getelementbyid(id).innerhtml = days + '. '; document.getelementbyid(id).innerhtml += hours + '. '; document.getelementbyid(id).innerhtml += minutes + '. '; document.getelementbyid(id).innerhtml += seconds + '.'; } timer = setinterval(showremaining, 1000); }
the problem code not if
, working:
if (seconds < 10) { document.getelementbyid(id).innerhtml += '0' + seconds + '.'; }
but fact that, after that, overwriting previous value:
document.getelementbyid(id).innerhtml += seconds + '.';
an easy solution converting seconds
string, in if
:
if (seconds < 10) { seconds = "0" + seconds; }
this way, can use string in subsequent innerhtml
.
Comments
Post a Comment