首先我是以秒来做单位,那我理清一个时间转换

            time.value = 3600;

            hours.value=Math.floor(time.value / 3600)

            minutes.value=Math.floor(time.value % 3600 / 60)

            seconds.value=Math.floor(time.value % 3600 % 60)

const startCountdown = () => {

    clearInterval(timer)

    if (time.value > 0) {

        let remainingTime = time.value;

        timer = setInterval(() => {

            if (remainingTime <= 0) {

                clearInterval(timer)

                showFailToast({ message: 'Time to drink water!', duration: 20000, closeOnClick: true, closeOnClickOverlay: true, overlay: true })

                return;

            }

            remainingTime--

            const hours = Math.floor(remainingTime / 3600);

            const minutes = Math.floor(remainingTime % 3600 / 60);

            const seconds = Math.floor(remainingTime % 3600 % 60);

            const formattedTime = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;

            document.querySelector('.interval').textContent = formattedTime;

            console.log(formattedTime)

        }, 1000)

    }else{

        document.querySelector('.interval').textContent =`00:00:00`;

    }

}

Logo

欢迎加入 MCP 技术社区!与志同道合者携手前行,一同解锁 MCP 技术的无限可能!

更多推荐