我正在尝试制作一种由用户触发的计时器,并且每分钟都会更新一次。这是我的代码!即使我添加了一个从差异中减去30k毫秒的间隔,计时器也永远不会关闭。
let x = await msg.channel.send('Calculating...')
async function countdownTimer() {
let difference = 1800000;
let remaining = "Monster Respawned!";
setInterval(() => {
difference = difference - 30000;
}, 30000);
if (difference > 0) {
const parts = {
days: Math.floor(difference / (1000 * 60 * 60 * 24)),
hours: Math.floor((difference / (1000 * 60 * 60)) % 24),
minutes: Math.floor((difference / 1000 / 60) % 60)
};
remaining = Object.keys(parts)
.map(part => {
if (!parts[part]) return;
return `${parts[part]} ${part}`;
})
.join(" ");
x.edit(`Respawn:${remaining}`);
}
}
countdownTimer()
setInterval(() => {
countdownTimer()
}, 60000);
```
答案 0 :(得分:0)
那呢:
let div = document.querySelectorAll(".out")[0];
let end = Date.now() + 30 * 60 * 1000; // end in 30 minutes
function countdownTimer() {
let remaining = "Monster Respawned!";
difference = end - Date.now();
if (difference > 0) {
const parts = {
days: Math.floor(difference / (1000 * 60 * 60 * 24)),
hours: Math.floor((difference / (1000 * 60 * 60)) % 24),
minutes: Math.floor((difference / 1000 / 60) % 60)
};
remaining = Object.keys(parts)
.map((part) => {
if (!parts[part]) return;
return `${parts[part]} ${part}`;
})
.join(" ");
div.innerText = `Respawn:${remaining} ${difference}`;
// recursion:
setTimeout(() => {
countdownTimer();
}, 5000);
} // if
}
// start:
countdownTimer();
请参阅:codepen