试图创建一个24小时计时器,重置自己

时间:2015-04-02 07:48:38

标签: javascript timer countdown

这是我的代码。非常凌乱,但由于我的经验不足,我无法察觉为什么它不起作用。根据我的计算,减量是标准的,至少是毫秒,秒和分钟,不确定时间。

这是代码。提前谢谢。

<!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>

2 个答案:

答案 0 :(得分:0)

你的间隔时间是错误的。您可以尝试以下代码。只需将你的变量放在你的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;
}

答案 1 :(得分:0)

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

我会在这里发布,以防任何人

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