SOURCE

console 命令行工具 X clear

                    
>
console
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>倒计时</title>

    <script src="https://unpkg.com/dayjs@1.8.21/dayjs.min.js"></script>
</head>

<body>
    <div>
        <p id="countdown1"></p>
        <p id="countdown2"></p>
    </div>

    <script>
        const countDown1 = (endTime) => {
            let day = "";
            let hour = "";
            let min = "";
            let second = "";

            let endTimeNumber = endTime;
            if (endTime && endTime.toString().indexOf(":") !== -1) {
                endTimeNumber = dayjs(endTime, 'HH:mm').valueOf();
            }

            let now = dayjs().valueOf();
            let end = endTimeNumber;

            let countdown_time = end - now;

            if (countdown_time >= 0) {
                day = Math.floor(countdown_time / (1000 * 60 * 60 * 24)).toString();

                let h = Math.floor((countdown_time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));

                hour = h.toString().padStart(2, '0');

                let m = Math.floor((countdown_time % (1000 * 60 * 60)) / (1000 * 60));
                min = m.toString().padStart(2, '0');

                let s = Math.floor((countdown_time % (1000 * 60)) / 1000);
                second = s.toString().padStart(2, '0');
            } else {
                day = '0';
                hour = "00";
                min = "00";
                second = "00";
            }

            if (Number(hour) === 0 && Number(day) === 0 && Number(min) === 0 && Number(second) === 0) {
                return '已超时';
            } else {
                let timeStr = day + "天" + hour + "小时" + min + "分钟" + second + "秒";
                return timeStr;
            }
        };

        const countDown2 = (endTime) => {
            console.log("�� ~ file: main.js:75 ~ countDown ~ endTime:", endTime);
            let day = "";
            let hour = "";
            let min = "";
            let second = "";

            let endTimeNumber = endTime;
            if (endTime && endTime.toString().indexOf(":") !== -1) {
                endTimeNumber = new Date(endTime).getTime();
            }

            let now = new Date().getTime();
            let end = endTimeNumber;
            console.log("�� ~ file: main.js:90 ~ countDown ~ end:", end);

            let countdown_time = end - now;
            console.log("�� ~ file: main.js:91 ~ countDown ~ countdown_time:", countdown_time);

            if (countdown_time >= 0) {
                day = Math.floor(countdown_time / (1000 * 60 * 60 * 24)).toString();

                let h = Math.floor((countdown_time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
                console.log("�� ~ file: main.js:97 ~ countDown ~ h:", h);
                hour = (h < 10 ? "0" + h : h).toString();

                let m = Math.floor((countdown_time % (1000 * 60 * 60)) / (1000 * 60));
                min = (m < 10 ? "0" + m : m).toString();

                let s = Math.floor((countdown_time % (1000 * 60)) / 1000);
                second = (s < 10 ? "0" + s : s).toString();
            } else {
                day = '0';
                hour = "00";
                min = "00";
                second = "00";
            }

            if (Number(hour) === 0 && Number(day) === 0 && Number(min) === 0 && Number(second) === 0) {
                return '已超时';
            } else {
                let timeStr = day + "天" + hour + "小时" + min + "分钟" + second + "秒";
                return timeStr;
            }
        };

        // 获取倒计时目标时间
        const endTime1 = dayjs().add(1, 'hour').add(30, 'minutes').valueOf();
        const endTime2 = dayjs().add(2, 'hours').add(45, 'minutes').valueOf();

        // 更新倒计时文本
        const updateCountdown = () => {
            const countdown1 = countDown1(endTime1);
            const countdown2 = countDown2(endTime2);

            document.getElementById('countdown1').innerText = countdown1;
            document.getElementById('countdown2').innerText = countdown2;
        };

        // 每秒更新一次倒计时文本
        setInterval(updateCountdown, 1000);

        // 初始化倒计时文本
        updateCountdown();
    </script>
</body>

</html>