Q:

javascript stopping a repeating setTimeout function

I have a function that functions as a countdown timer for a simple pomodoro clock I'm building. Now I want to stop this function when the user clicks a reset function. I though I'd pass it a simple if statement that returns something and therefore terminates the function before the countdown iterates again but the function keeps on going. Any idea what's going on here is appreciated. Code below, can also check the whole thing on codepen.
  function countdown(blocker) {
    if (blocker === true) {
      return true;
    } 
    else {
      setTimeout(function() {
        time -= 1;
        updateTimer();
        if (time > 0) {
          countdown();
        }
      }, 1000);
    }
  }

calling the function like this doesn't stop it once it gets going:
countdown(true);

A:

Don't need to use setInterval, as suggested by K Ф, But you do need to clear the timeout with clearTimeout. Make sure you store a reference to your timeout object:
timeout = setTimeout(/* arguments */);

And add a reset function and use it when resetting:
function reset () {

  if(timeout)
    clearTimeout(timeout);

  time = 1500;

}

Here's your Pen updated.

