首先我是以秒来做单位,那我理清一个时间转换
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`;
}
}