Hey all. I’m working on my pomodoro clock and for some reason, when it’s counting down the break time, it skips zero. I have two timer functions, “timer” (declared on line 38) and “breakTimer” (line 49). The timer works just fine, and when it hits zero it does some stuff, including triggering the breakTimer function. But for some reason the breakTimer function goes down to 1, then skips 0 and moves on to -1, thus never allowing the clearInterval to happen and counting down indefinitely. I can’t seem to tell why one function works and the other doesn’t when they are written more or less exactly the same. Any help would be very much appreciated. Link to my pen here.
bonus: I don’t like that my breakTimer function is declared within the timer block, but I can’t seem to figure out where to put it in order to make everything work the same. Where can I put the breakTimer function so that it will stlil have access to the variables it needs?
The error that jumps out at me is that you declare breakCounter
in your timer
function, but then reference it in breakTimer
where it isn’t declared so setInterval(breakCounter)
on line 55 has an UncaughtReferenceError.
Thanks for your reply! I tried moving the breakCounter declaration to line 38, outside the timer function, but now the breakTimer function isn’t getting triggered.
You could declare it at line 38 let breakCounter;
and then assign it at line 47 breakCounter = setInterval(breakTimer, 1000)
.
Alternatively you could use var
as it is functionally scoped not block scoped but that’s against best practices.
This fixed it! Thank you so much @KevinReynolds! To prevent going too deep down a rabbit hole and losing work, I applied your fixes in a fork, now live at https://codepen.io/kaydeejay/pen/OvwWdq.