尝试创建一个可自行重置的 24 小时计时器

Trying to create a 24 hour timer that resets itself

这是我的代码。非常混乱,但由于我缺乏经验,我无法发现它为什么不起作用。根据我的统计,Decrements 是 js 标准的,至少是毫秒、秒和分钟,不确定小时数。

这是代码。提前致谢。

<!DOCTYPE html>
<html>
<body>

<span id="tHours"></span>:<span id="tMins"></span>:<span id="tSeconds"></span>:<span id="tMilli"></span>

<script>
    var hours = 1;
    var mins = hours * 60;
    var secs = mins * 60;
    var mill = secs * 100;
    var currentHours = 0;
    var currentSeconds = 0;
    var currentMinutes = 0;
    vas currentMilli = 0;
    setTimeout('DecrementMilli()',100);
    setTimeout('DecrementSeconds()',1000);
    setTimeout('DecrementMinutes()',10000);
    setTimeout('DecrementHours()',100000);

    function DecrementMilli() {
        currentMilli = secs % 100;
        if(currentMilli <= 99) currentMilli = "000" + currentMilli;
        secs--;
        document.getElementById("tMilli").innerHTML = currentMilli; 
        if(mill !== -1) setTimeout('Decrement()',100);
    }
        function DecrementSeconds() {
        currentSeconds = secs % 60;
        if(currentSeconds <= 9) currentSeconds = "0" + currentSeconds;
        secs--;
        document.getElementById("tSeconds").innerHTML = currentSeconds; 
        if(secs !== -1) setTimeout('Decrement()',1000);
    }
        function DecrementMinutes() {
        currentMinutes = Math.round(secs / 60);
        if(currentMinutes <= 60) currentMinutes = "00";
        mins--;
        document.getElementById("tMins").innerHTML = currentMinutes; 
        if(mins !== -1) setTimeout('Decrement()',10000);
    }
        function DecrementHours() {
        currentHours = Math.round(1440 / 60);
        if(currentHours <= 24) currentHours - 1;
        hours--;
        document.getElementById("tHours").innerHTML = currentHours; 
        if(hours !== -1) setTimeout('Decrement()',100000);
    }
</script>

</body>
</html>

你的时间间隔是错误的。你可以试试下面的代码。只需将变量放入 html,例如:

<span id='tHours'>23</span>:<span id='tMins'>59</span>:<span id='tSeconds'>59</span>:<span id='tMilli'>99</span>

而 js 是这样的:

var milli = 99;
var sec = 59;
var min = 59;
var hour = 23;

setInterval(function () {
    milli = milli == 0 ? 99 : milli - 1;
    $('#tMilli').text(double0(milli));
},10);

setInterval(function () {
    sec = sec == 0 ? 59 : sec - 1;
    $('#tSec').text(double0(sec));
},1000);

setInterval(function () {
    min = min == 0 ? 59 : min - 1;
    $('#tMin').text(double0(min));
},60000);

setInterval(function () {
    hour = hour == 0 ? 23 : hour - 1;
    $('#tHour').text(double0(hour));
},1440000);

function double0 (num) {
    num = num.toString().length == 1 ? '0' + num : num;
    return num;
}

好吧,我解决了我自己的问题,但它并不优雅,因为它不是从 24:00:00 开始,而是从 23:59:59 开始。但这是一个开始。

我会post这里,以防它对任何人有帮助

<script type="text/javascript">
var count = 86400;
var counter = setInterval(timer, 1000);

function timer() {
    count = count - 1;
    if (count == -1) {
        clearInterval(counter);
        return;
    }

    var seconds = count % 60;
    var minutes = Math.floor(count / 60);
    var hours = Math.floor(minutes / 60);
    minutes %= 60;
    hours %= 60;

    document.getElementById("timer").innerHTML = hours + ":" + minutes + ":" + seconds; // watch for spelling
}
</script>



<span id='timer'></span>