Struggling with Pomodoro clock!

Hi All,

I’ve been at the pomodoro clock for a while now, and thought it might be time to reach out to the forums just to get a hint or an idea of how I’m going about this wrong.

I’m able to get an actual timer going…once. No matter what I try the timer runs and then the break timer ends up running at the same time. I’ve tried making if statements to more or less have an ‘on/off’ switch to have one work, and then the other one go afterwards but no dice.

I started researching how to get two timers to work, and it appears that one of the things with JavaScript is that it is a single threaded language, meaning (correct me if I’m wrong) all the code runs at once. So you essentially cannot have this start/stop kinda methodology that I’m trying to implement with the different timers.

So what do I need to do? How can I get one timer to work, stop, then active the next timer? I’m fairly confident in my ability to spin up some code and logic, but I believe this challenge is a higher level concept of how JavaScript works overall (great idea for a challenge honestly). Any hints or points in the right direction would be greatly appreciated!!


1 Like

Hi Seth,

Redacted from post: --I am pretty sure you are correct that you can only have one window.setInterval() running in Javascript. However, you can still use that single time function…–
Correction: While it is possible to have to Javascript timers (i.e. window.setInterval() running at once, you can still use a single setInterval() time function to keep track of two timers, i.e. your “session time” and your “break time”. Here is an outline of how that might work:
-you have a boolean to keep track of whether or not the “session time” is running, and another boolean to keep track of whether or not the “break time” is running.
-then you have a variable that stores and counts the seconds remaining in session time, and another variable storing and counting the remaining seconds in break time.
-With these two counting variables and two boolean variables , you can have window.setInterval() running the entire time in the background, and then manipulate and check the logic of the booleans to decided whether or not to decrease the seconds count in session time or break time. For example, if the boolean sessionTimeIsRunning is set to true, then, on each interval of setInterval, decrease the number of seconds in the sessionTimeSecondsRemaining variable. If the boolean is false, then don’t decrease sessionTimeSecondsRemaining.

This is a very general outline. Let me know if there is something you don’t understanding about it - I am happy to clarify.

1 Like

I wrote this a while back, it is the basic structure of what I used to solve that challenge, maybe it will help you:

set interval for work()
    do whatever ==> display time , graphics , etc...
    check time
       if time is not up ==>  keep on doing whatever ==> display time , graphics , etc...
       if time is up ==> Clear work interval, start Break interval

set interval for break()
    do whatever ==> display time , graphics , etc...
    check time
       if time is not up ==>  keep on doing whatever ==> display time , graphics , etc...
       if time is up==> Clear break interval, start Work interval

Thank you both for the responses! I’ll take this to the drawing board and see what I can make of it :smiley:

Who says you can not run two setIntervals at the time?

var counter1 = 0;
var timer1 = window.setInterval(function(){
  console.log("First Timer");
  if (counter1++ > 10) clearInterval(timer1);
}, 1000);

var counter2 = 0;
var timer2 = window.setInterval(function(){
  console.log("Second Timer");
   if (counter2++ > 10) clearInterval(timer2);
}, 1500);

Thanks for the correction @RandellDawson! I’ve edited my post to remove the misinformation.

Thanks for the clarification on this, that makes sense! I actually ended up getting the two timers to work with one setInterval with what Marcus wrote. Now I’m just struggling on how to pause and get the timer going again.