Pomdoro Clock counting down two numbers?

Pomdoro Clock counting down two numbers?
0

#1

Hey guys I’m working on my pomdoro clock with jQuery. I have almost finished but when my timer is paused and then played after the initial start it starts to count down two values in the seconds area.

This is my countDown function:

function countDown(m,s) { 
  runTime = setInterval(function() {
    if(isStarted) {
    if (m == 0 && s == 0) {
        stopTimer();
        if (loop == 0) {
            timeLeft = break_interval_m;
            loop += 1;
            $('#timer-label').text('Break');
        } else {
            timeLeft = ses_interval_m;
            loop -= 1;
            $('#timer-label').text('Session');
        }
        alarm.play();
        countDown(timeLeft,0);
    } else if (s != 0) {
        if (s <= 10){
            s -= 1;
            timeLeft = m + ':0' + s;
        } else {
            s -= 1;
            timeLeft = m + ':' + s;
        }
    } else if (s == 0) {
        s = 59;
        m -= 1;
        timeLeft = m + ':' + s;
    }
    
        $('#time-left').text(timeLeft);  
    }
  }, 1000);
} 

and this is my start_stop click function:

$('#start_stop').click(function() {
    start_stop_counter++
    if (Number.isInteger(start_stop_counter/2)){
      isStarted = false;
      pauseButton();
 
    }
    else {
      isStarted = true; 
      playButton(); 
      countDown(ses_interval_m, ses_interval_s)
    }
  })

The code seems to be decrementing two values at once. What am I missing?
Here is the full code


#2

You have to make sure you clear each interval every time time you stop, so the next time you click the start button, you will only have a single timer running.


#3

Yeah that’s what I had it originally but the problem I found if I do that is whenever I pause the timer and play it again the timer will reset because the interval cleared. The code now was my attempt to pause the interval while stopped and resume it when the play button is pressed again. Is there a way to pause and start the timer when I clear the interval?


#4

Below is where you deal with clicks of the start button.

    $("#start_stop").click(function() {
      start_stop_counter++;
      if (Number.isInteger(start_stop_counter / 2)) {
        isStarted = false;
        pauseButton();
      } else {
        isStarted = true;
        playButton();
        countDown(ses_interval_m, ses_interval_s);
      }
    });

Did you try adding stopTimer() to the if statement code block?

On a side note, the following is an odd way to determine if you need to pause or play.

if (Number.isInteger(start_stop_counter / 2)) {

Why not just base it on the Boolean isStarted? Then after the if/else statement, you could simply negate isStarted like !isStarted.


#5

Okay I fixed the pause play determination. stopTimer() just executes the clearInterval() on my timer so when I added it to the if/else statement it reset the timer to the initial time. I added the boolean isStarted to the setInterval() function to pause it. Is that bad?


#6

Okay I fixed the issue. The problem was that I declared ses_interval_m and ses_interval_s. I used these variables in my countDown function but did not declare them as the new minute and seconds at the end of the function. Thanks for the help Randell!