简单的JavaScript计时器

时间:2016-01-28 11:25:59

标签: javascript html

我正在尝试创建一个非常简单的JavaScript倒计时器,我在显示"分钟"时遇到问题。用字符串" 0"当它只有1位数时,它会添加" 0"每秒钟#34;秒"不要添加更多" 0",只有一个" 0"当它显示1位数字时,如何只显示一个" 0"一分钟?

问候。

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>count down test</title>
</head>
<body>

<p id="demo"></p>

<script>
var seconds = 10; //10 sec just to test, originally 60
var minutes = 5; //10 min just to test, originally 59
var hour = 1; //can be set to 48 hours for 2days, 72 hours for 3days

//function to display time
function count() {
    if(hour == 0 && minutes == 0 && seconds == 0) {
        clearInterval();
    }

    //reset seconds when reached 0
    if(seconds == 0) {
        seconds = 10; //originally 60
        minutes--;
    }

    if(minutes == 0) {
        minutes = 10; //originally 59

        if(hour != 0) {
            hour--;
        }
    }

    //-1sec every second
    seconds--;

    //if less than 10 sec add "string" character 0 to first digit
    if(seconds < 10) {
        seconds = "0" + seconds;
    }

    //if less than 10 min add "string" character 0 to first digit
    if(minutes < 10) {
        minutes = "0" + minutes;
    }

    document.getElementById("demo").innerHTML = hour + " : " + minutes + " : " + seconds;
}

count();

setInterval(function(){
    count();
}, 1000);
</script>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

当分钟数小于10时,添加该字符串&#34; 0&#34;到orignal数据类型编号分钟并将其转换为字符串类型并重复它,这就是为什么它一次又一次地添加0像字符串你只需要parseFloat()将orignal分钟返回到数字类型,这样js就不会添加为零每一秒再次串起..希望有意义的是你可以检查下面的代码再次测试它     //你这样做     //错误的案例

//if less than 10 sec add "string" character 0 to first digit
if(seconds < 10) {
    seconds = "0" + seconds;
}

//if less than 10 min add "string" character 0 to first digit
if(minutes < 10) { 
    minutes = "0" + minutes;
}

// DO it like this
// RIGHT CASE
//if less than 10 sec add "string" character 0 to first digit
if(seconds < 10) {
seconds = "0" + seconds;
}

//if less than 10 min add "string" character 0 to first digit
if(minutes < 10) {
minutes = "0" + parseFloat(minutes);
}

答案 1 :(得分:0)

不要更改秒和分钟变量,它会影响条件

我已将代码修改为您的要求,请查看this

    function count() {
                var placerS,placerM;
                if(hour == 0 && minutes == 0 && seconds == 0) {
                clearInterval();
                }

                //reset seconds when reached 0
                if(seconds == 0) {
                seconds = 10; //originally 60
                minutes--;
                }

                if(minutes == 0) {
                minutes = 10; //originally 59

                if(hour != 0) {
                hour--;
                }
                }

                //-1sec every second
                seconds--;

                //if less than 10 sec add "string" character 0 to first digit
                if(seconds < 10) {
                placerS = "0";
                }else{
                placerS = '';
                }

                //if less than 10 min add "string" character 0 to first digit
                if(minutes < 10) {
                placerM = "0";
                }else{
                placerM = '';
                }

                document.getElementById("demo").innerHTML = hour + " : "+placerM + minutes + " : "+placerS + seconds;
    }

答案 2 :(得分:0)

您可以使用以下代码转换分钟:

if(minutes < 10) 
{
    var totalmin;
    var init = 0;
    var min = parseFloat(minutes);
    totalmin= init  + min;
    minutes = totalmin.toString();
}