efuzz
efuzz

Reputation: 117

How to make Javascript Count Up timer work

I am trying to implement a simple count up timer using Javascript and show the timer in the HTML page. This is my code:

var minutesLabel = document.getElementById("minutes");
var secondsLabel = document.getElementById("seconds");
var totalSeconds = 0;
setInterval(setTime, 1000);

function setTime()
{
    ++totalSeconds;
    secondsLabel.innerHTML = pad(totalSeconds%60);
    minutesLabel.innerHTML = pad(parseInt(totalSeconds/60));
}

function pad(val)
{
    var valString = val + "";
    if(valString.length < 2)
    {
        return "0" + valString;
    }
    else
    {
        return valString;
    }
}

HTML:

<label id="minutes">00</label>
    <label id="colon">:</label>
    <label id="seconds">00</label>

But the timer is not working in the HTML. It stays at 00:00

Upvotes: 3

Views: 62

Answers (3)

efuzz
efuzz

Reputation: 117

Make sure the script is at the bottom on the body tag

Upvotes: 2

anpel
anpel

Reputation: 941

The code you posted works exactly as you describe.

Check this fiddle, I just copy pasted it.

.

Upvotes: 1

kishore Rajendran
kishore Rajendran

Reputation: 278

The code you mentioned is working perfectly fine in the snippets.

Upvotes: 0

Related Questions