从计时器进行倒计时

Make a countdown from timer

本文关键字:倒计时 计时器      更新时间:2023-09-26

有没有办法用 60 秒进行倒计时......这是计时器的代码:

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

我需要整理什么才能使这段代码像倒计时一样>谢谢

计数从

0 到 60。

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

或从 60 到 0:

var count = 60, timer = setInterval(function() {
    $("#counter").html(count--);
    if(count == 1) clearInterval(timer);
}, 1000);
var count = 60;
var timer = $.timer(function() {
    $('#counter').html(--count);
});
timer.set({ time : 1000, autostart : true });
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!!!");
    }
}