-1

Here is a simple timer, how would I implement clearInterval() in this bit of code when the timer reaches 0? It currently is infinite.

const start = 0.1; //6 seconds
let time = start * 60;

const count = document.querySelector('#countdown-timer');
const interval = setInterval(updateTimer, 1000);

function updateTimer() {
  const minutes = Math.floor(time / 60);
  let seconds = time % 60;

  seconds = seconds < 10 ? '0' + seconds : seconds;
  count.innerHTML = `${minutes}:${seconds}`;
  time--;
}
<span id="countdown-timer"></span>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Josh
  • 110
  • 1
  • 7

1 Answers1

0

Like this

Also you can simplify the padding

const start = 0.1; //6 seconds
let time = start * 60;

const count = document.querySelector('#countdown-timer');
const interval = setInterval(updateTimer, 1000);

function updateTimer() {
  if (time<=0) clearInterval(interval)
  const minutes = Math.floor(time / 60);
  let seconds = time % 60;
  count.innerHTML = `${minutes}:${String(seconds).padStart(2,'0')}`;
  time--;
}
<span id="countdown-timer"></span>
mplungjan
  • 169,008
  • 28
  • 173
  • 236