Pomodoro loops instead of stops

so I’ve got my pomodoro to reset when you hit the button but when It hits zero it restarts. I was expecting it to just end, but it starts over… I suspect it’s in the scope of the ‘ifs’ around lines 18 or 23…

var existingIntervalId = 0;
function startTimer(duration, display) {
  var start = Date.now(), diff, minutes, seconds;
  function timer() {
    // get the number of seconds that have elapsed since
    // startTimer() was called
    diff = duration - (((Date.now() - start) / 1000) | 0);

    // does the same job as parseInt truncates the float
    minutes = (diff / 60) | 0;
    seconds = (diff % 60) | 0;

    minutes = minutes < 10 ? "0" + minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;

    display.textContent = minutes + ":" + seconds;

    if (diff <= 0) {
      // add one second so that the count down starts at the full duration
      // example 05:00 not 04:59
      start = Date.now() + 1000;
    }
    if (diff === 0) {
      // add one second so that the count down starts at the full duration
      clearInterval(existingIntervalID);
    }
  }
  // we don't want to wait a full second before the timer starts
  timer();
  existingIntervalId = setInterval(timer, 1000);
}

$("#twentyfive").on("click", function() {
  var fiveMinutes = 60 * 0.1, display = document.querySelector("#time");
  // Clear existing interval
  clearInterval(existingIntervalId);
  // Start the timer
  startTimer(fiveMinutes, display);
});

https://codepen.io/TurtleWolf/pen/xrxOpL?editors=0010

You got a typo there – the last D should be lowercase. Watch the console! ;-)

1 Like

thank you!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.