Jake Jake - 6 months ago 25
HTML Question

Javascript, countdown timer and display text

I am making a countdown timer with JavaScript.

Here is my script.

var seconds_left = 10;
var interval = setInterval(function() {
document.getElementById('timer_div').innerHTML = --seconds_left;

if (seconds_left <= 0)
{
//When it gets to 0 second, I want to show 'You are Ready!' text message.

}
}, 1000);


It starts to count from 10 seconds.

I want to eliminate seconds when it gets 0 second and show a 'You are Ready!' message.

Can anyone help?

Answer
var seconds_left = 10;
var interval = setInterval(function() {
    document.getElementById('timer_div').innerHTML = --seconds_left;

    if (seconds_left <= 0)
    {
        document.getElementById('timer_div').innerHTML = 'You are ready';
        clearInterval(interval);
    }
}, 1000);

Here is the Example

Comments