Make a countdown from timer

Peter Web picture Peter Web · May 10, 2012 · Viewed 9.1k times · Source

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

Answer

Elliot Bonneville picture Elliot Bonneville · May 10, 2012

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);