通过JavaScript脚本实现60秒倒计时

  • JS 代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CountDown</title>
</head>
<body>
    <span class="second">开始</span>

    <script>
            let second = document.querySelector(".second");
            let i = 60  // 倒计时秒数

            // 定时器 每隔一秒变化一次(1000ms = 1s)
            let t = setInterval(countDown, 1000);

            function countDown() {
                    console.log(i);
                    second.innerHTML = i;

                    // 60 秒倒计时结束
                    if (i === 0)
                        clearInterval(t);

                    i--;
        }
    </script>
</body>
</html>

你可能感兴趣的:(Tools,javascript,前端,计时器,sleep函数,实用工具)