暂停与时钟恢复JavaScript计时器
我创建了一个应用程序,用户可以输入时间,计时器和时钟手将一致倒计时。
但是,当我停下来恢复时,计时器UI按预期工作,但时钟可以回到其静止位置,而不是继续从暂停的位置继续进行。
用户应该能够随时输入,并且时钟手应该反映输入的时间与剩余时间之间的差异 - 如进度栏,从100%到0%。
如何在时钟修复此暂停 +简历行为?
let remaining = duration
let timer = null
let end = 0
// Start button
startButton.addEventListener('click', e => {
// Continue if timer is running
if (timer) return
duration = (hoursEntered * 3600000) + (minutesEntered * 60000) + (secondsEntered * 1000);
remaining = duration
// Get end timestamp based on current time + remaining time
end = Date.now() + remaining
// Render the remaining time every 16ms (approx 60fps)
timer = setInterval(() => {
// If remainisng time is zero, stop clock
if (end - Date.now() <= 16) {
clearInterval(timer);
timer = 0;
showStartButton()
} else {
render(end - Date.now())
animateRing();
}
}, 16)
})
// Pause button
pauseButton.addEventListener('click', e => {
// Do nothing if timer not running
if(!timer) return
// Otherwise, clear timer
clearInterval(timer)
timer = null
// Note the remaining time
remaining = end - Date.now()
render(remaining)
})
// Divide time left by time remaining
const calcPercent = () => {
return percent = (end - Date.now()) / remaining;
};
// Update the ring and clockhand as time passes, starting with 283
// Where the length of arc = 2πr = 2 * π * 45 = 282.6
const animateRing = () => {
const ringArray = `${(calcPercent() * ring).toFixed(0)} 283`;
document.getElementById('base-timer-path-remaining').setAttribute('stroke-dasharray', ringArray);
// Animate clockhand
document.getElementById('clockhand').style.transform = 'rotate(-' + (calcPercent() * 360) + 'deg)';
};
render(remaining)
I created an app where users can input time, and a timer and clockhand will countdown in unison.
When I pause then resume, however, the timer UI works as anticipated, but the clock resets back to its resting position instead of continuing from its paused position.
The user should be able to input any time, and the clockhand should reflect the difference between the time entered and the time remaining--actinig like a progress bar, running from 100% to 0%.
How can I fix this pause + resume behavior in the clock?
JavaScript MVP
let remaining = duration
let timer = null
let end = 0
// Start button
startButton.addEventListener('click', e => {
// Continue if timer is running
if (timer) return
duration = (hoursEntered * 3600000) + (minutesEntered * 60000) + (secondsEntered * 1000);
remaining = duration
// Get end timestamp based on current time + remaining time
end = Date.now() + remaining
// Render the remaining time every 16ms (approx 60fps)
timer = setInterval(() => {
// If remainisng time is zero, stop clock
if (end - Date.now() <= 16) {
clearInterval(timer);
timer = 0;
showStartButton()
} else {
render(end - Date.now())
animateRing();
}
}, 16)
})
// Pause button
pauseButton.addEventListener('click', e => {
// Do nothing if timer not running
if(!timer) return
// Otherwise, clear timer
clearInterval(timer)
timer = null
// Note the remaining time
remaining = end - Date.now()
render(remaining)
})
// Divide time left by time remaining
const calcPercent = () => {
return percent = (end - Date.now()) / remaining;
};
// Update the ring and clockhand as time passes, starting with 283
// Where the length of arc = 2πr = 2 * π * 45 = 282.6
const animateRing = () => {
const ringArray = `${(calcPercent() * ring).toFixed(0)} 283`;
document.getElementById('base-timer-path-remaining').setAttribute('stroke-dasharray', ringArray);
// Animate clockhand
document.getElementById('clockhand').style.transform = 'rotate(-' + (calcPercent() * 360) + 'deg)';
};
render(remaining)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
按下“开始”按钮时,Calccent总是1。您将剩余时间除以剩余时间(end = date.now() +剩余时间与end -date.now()=剩余时间相同。我建议重写算法中有几种等效的表达式。但是,下面的代码正常运行您想要的方式,我在小提琴中进行了测试。
calcPercent was always 1 when you hit the start button. You were dividing the remaining time by the remaining time (end = Date.now() + remaining is the same as end - Date.now() = remaining). You have several equivalent expressions in your algorithm, I suggest a rewrite. However, the code below is functioning the way you want, I tested in your fiddle.