Peter Web
Peter Web

Reputation: 359

Make a countdown from timer

Is there any way to make a countdown with 60 seconds... Here is the code for timer:

var count = 0;
var timer = $.timer(function() {
    $('#counter').html(++count);
});
timer.set({ time : 1000, autostart : true });

What I need to chabge to make this code like countdown> THANKS

Upvotes: 5

Views: 18335

Answers (3)

strah
strah

Reputation: 6722

var timer;
var count = 60;

$("#counter").text(count);
//update display

timer = setTimeout(update, 1000);
//this allows for 'clearTimeout' if needed

function update()
{
    if (count > 0)
    {
       $("#counter").text(--count);
       timer = setTimeout(update, 1000);
    }
    else
    {
        alert("Done!!!");
    }
}

Upvotes: 0

Danilo Valente
Danilo Valente

Reputation: 11342

var count = 60;
var timer = $.timer(function() {
    $('#counter').html(--count);
});
timer.set({ time : 1000, autostart : true });

Upvotes: 0

Elliot Bonneville
Elliot Bonneville

Reputation: 53291

Counts from 0 to 60.

var count = 0, timer = setInterval(function() {
    $("#counter").html((count++)+1);
    if(count == 59) clearInterval(timer);
}, 1000);

Or from 60 to 0:

var count = 60, timer = setInterval(function() {
    $("#counter").html(count--);
    if(count == 1) clearInterval(timer);
}, 1000);

Upvotes: 19

Related Questions