js 倒计时01

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            width: 200px;
            height: 200px;
            margin: 0 auto;
            text-align: center;
            font-size: 60px;
            line-height: 200px;
            border: 5px solid #ccc;
        }

        body {
            text-align: center;
        }

        input {
            width: 210px;
            height: 50px;
        }
    </style>
</head>

<body>
    <div class="box">
        100
    </div>
    <input type="submit" value="开始" id="sub">
</body>
<script>
    // 获取元素
    var sub = document.querySelector("#sub");
    var div = document.querySelector(".box");
     // 定义计时器
     var t;
     // 定义开关
     var i = 0;
    // 绑定点击事件
    sub.onclick = function () {
        if(i==0){
              if (i == 0) {
                // 开启计时器
                t = setInterval(function () {
                    if (div.innerHTML <= 0) {
                        clearInterval(t);
                        sub.value="复位";
                        i=2;
                    } else {
                        div.innerHTML--;
                        sub.value="暂停";
                    }
                }, 30);
            }
            i=1;
        }else{
            if(i==1){
                clearInterval(t);
                i=0;
            }else{
                div.innerHTML=100;

                i=0;
            }
        }   
          
}
</script>

</html>

猜你喜欢

转载自blog.csdn.net/qq_45555960/article/details/102513944