我被Pomodoro时钟的计时器功能所困,我希望循环继续运行,并从每次使用已设置的当前值开始。
$("#start_stop").click(function() {
sessionCounter = setInterval(sessionTimer, 1000); // Run this function every 1 second
sessionLength *= 60; // Turn seconds into minutes
function sessionTimer() {
sessionLength -= 1;
if (sessionLength === 0) {
// ToDO audio file
clearInterval(sessionCounter); // Clear interval once sessionLength is 0
breakCounter = setInterval(breakTimer, 1000); // Run this function every 1 second
breakLength *= 60;
}
$("#timer-label").html("Session Time Left"); // Change Label on Timer Start
if (sessionLength % 60 >= 10) {
$("#time-left").html(
Math.floor(sessionLength / 60) + ":" + (sessionLength % 60)
);
} else {
$("#time-left").html(
Math.floor(sessionLength / 60) + ":" + "0" + (sessionLength % 60)
);
}
function breakTimer() {
clearInterval(sessionCounter);
breakLength -= 1;
if (breakLength === 0) {
clearInterval(breakCounter); // Clear interval
setInterval(sessionTimer, 1000);
$("#time-left").html(sessionLength);
}
$("#timer-label").html("Break Time Left"); // Change Label on Timer Start
$("#time-left").html(breakLength); // Update HTML
if (breakLength % 60 >= 10) {
$("#time-left").html(
Math.floor(breakLength / 60) + ":" + (breakLength % 60)
);
} else {
$("#time-left").html(
Math.floor(breakLength / 60) + ":" + "0" + (breakLength % 60)
);
}
}
}
到目前为止,我已经拥有了,现在一旦中断长度达到0,它就会再次启动会话计时器,尽管它的值为负值并不断计数。
我本质上需要的是,一旦breakLength === 0,我想再次运行sessionTimer函数,但是要重新设置间隔,然后一旦sessionLength === 0,就重复中断定时器。