-2
function countdown()
{
    seconds = 1000;
    delay = delay-seconds;
    conversion = Math.ceil(delay / seconds );
    document.getElementById('timer').innerHTML =  conversion + ":seconds until alarm goes off";
    t3 = setTimeout("countdown()",1000);

    if(conversion == 0)
    {
        alert('Wake UP!!!');
        DontDoIt();
    }
}

此代码以秒为单位显示计时器。如果时间大于 60,我希望能够以分钟和秒为单位显示时间。

我希望能够用小时、分钟和秒来做到这一点

4

1 回答 1

0

这应该可以为您解决问题 如果需要,它会显示小时、分钟(带前导零)和秒

function countdown() {
    seconds = 1000;
    delay = delay - seconds
    secs = Math.ceil(delay / seconds);
    var hrs = Math.floor(secs / 3600);
    var mins = Math.floor((secs - (hrs * 3600)) / 60);
    secs -= ((hrs * 3600) + (mins * 60));

    document.getElementById('timer').innerHTML = ((hrs>0) ? hrs + ':' : '') + ((mins>0) ? ((mins>9) ? mins : '0'+mins) + ':' : '') + ((secs>9) ? secs : '0'+secs) + " until alarm goes off";
    t3 = setTimeout("countdown()", 1000);
    if (delay == 0) {
        alert('Wake UP!!!');
        DontDoIt();
    }
}
于 2013-02-01T17:40:29.787 回答