Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 484
Jake Avatar asked Apr 03 '12 07:04

Jake


2 Answers

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

like image 107
safarov Avatar answered Nov 20 '22 20:11

safarov


document.getElementById('timer_div').innerHTML = "You are Ready!";
like image 38
Emil Vikström Avatar answered Nov 20 '22 19:11

Emil Vikström