2

so I have this countdown timer I created,

<script type="text/javascript">
var interval;
    var minutes = 12;
    var seconds = 32;
    window.onload = function() {
        countdown('countdown');
}

function countdown(element) {
    interval = setInterval(function() {
        var el = document.getElementById(element);
        if(seconds == 0) {
            if(minutes == 0) {               
                minutes=7;
seconds=47;
                } else {
                    minutes--;
                    seconds = 60;
                }
            }
            if(minutes > 0) {
                var minute_text = minutes + (minutes > 1 ? ':' : ':');
            } else {
                var minute_text = '';
            }
            var second_text = seconds > 1 ?'':'';
            el.innerHTML = minute_text + ''+seconds + ' '+second_text + ' remaining';
            seconds--;
        }, 1000);

}
</script> 

The result works perfectly, but when the timer reaches digits below 10, (12:03, 12:05, etc.) it displays the seconds without a '0' in the front. (12:3 instead of 12:03)

I tried fixing this, but it has brought me nowhere. Is it possible to somehow edit my script to fix this bug?

Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
  • 1
    possible duplicate of [How can I create a Zerofilled value using JavaScript?](http://stackoverflow.com/questions/1267283/how-can-i-create-a-zerofilled-value-using-javascript) – sachleen Dec 27 '12 at 20:00

5 Answers5

1

A number does not have a zero in front of it.

You have to manually add that.

if(seconds < 10) {
    second_text = "0" + seconds;
}
sachleen
  • 30,730
  • 8
  • 78
  • 73
Naftali
  • 144,921
  • 39
  • 244
  • 303
1
second_text = (seconds > 9) ? (seconds) : ('0' + seconds);
John Doe
  • 173
  • 10
1

I just cleaned your code a bit:

var interval,
    minutes = 12,
    seconds = 32;

window.onload = function() {
    countdown('countdown');
}

function countdown(element) {
    var el = document.getElementById(element),
        minutes_text, second_text;

    interval = setInterval(function() {
        if(seconds == 0) {
            if(minutes == 0) {
                minutes=7;
                seconds=47;
            }
            else {
                minutes--;
                seconds = 60;
            }
        }

        minutes_text = minutes;
        second_text = (seconds < 10 ? "0" : "") + seconds;

        el.innerHTML = minutes_text + ':' + second_text + ' remaining';
        seconds--;
    }, 1000);
}
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
0

try adding a function to pad the value displayed

function padvalue(i) {
    if (i<10) {
        i="0" + String(i);
    }
    return i;
}

Call it here

el.innerHTML = minute_text + '' + padvalue(seconds) + ' ' + second_text + ' remaining';
vortextangent
  • 561
  • 4
  • 16
0

Use this snippet to convert the numbers to strings:

var secondStr = seconds + "";
while (secondStr.length < 2) {
    secondStr = "0" + secondStr;
}

Do the same thing for minutes and hours and then print the strings.

(You do know that doing an interval of 1000 means the interval will be 1000 or more milliseconds? It's unlikely that you miss a whole tick though with that long of an interval. This means the counter will be close to real time seconds but not exact and will vary from time to time and computer to computer.)

Lee Meador
  • 12,829
  • 2
  • 36
  • 42