我正在尝试构建一个带有暂停选项的番茄钟计时器。其中包括一个模拟时钟和一个数字计时器。我的问题在于数字计时器 - 我可以通过清除间隔来暂停它,但不知道如何在不重新开始(使用新的 setInterval)的情况下恢复它。
这是项目的codesandbox。
以下是 DigitalClock 组件的相关部分:
这是项目的codesandbox。
以下是 DigitalClock 组件的相关部分:
const timer = () => {
const now = Date.now()
const then = now + mode.duration * 60 * 1000
countdown = setInterval(() => { // That's how I resume it (with a re-render)
const secondsLeft = Math.round((then - Date.now()) / 1000)
if(secondsLeft < 0 || pause) {
clearInterval(countdown) // That's how I pause it (by clearing the interval)
return;
}
displayTimeLeft(secondsLeft)
}, 1000)
}