Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

375
Vistas
Why my interval is still counting and didn't stop at 0?

I wonder why my countdown didn't stop at 0, the "Time's Up" log are still logging infinitely.

Here's my code:

let timer = 6;
setInterval(function () {
  if (timer > 0) {
    timer--;
  console.log(timer);
  } else {
    console.log("Time's Up");
    clearInterval(timer);
  }
}, 1000);
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

clearInterval needs to know the action you want to cancel. In this case, the action is actually your setInterval, so just assign it to a variable and use that variable as a parameter for clearInterval.

 const myInterval = setInterval(() => {
  if (timer > 0) {
    timer--;
  console.log(timer);
  } else {
    console.log("Time's Up");
    clearInterval(myInterval);
  }
}, 1000); 
about 4 years ago · Juan Pablo Isaza Denunciar

0

What I understand that you are using timer as a variable. So, you are doing one mistake, you are giving wrong parameter inside clearInterval method. You can stop clearInterval method by the code written below:-

NOTE:- I just substitute console.log with document.write to show the Output on screen.

var timer = 5;
var myinterval = setInterval(function () {
    if (timer > 0) {
      timer--;
    document.write(timer+"<br>");
    } else {
      document.write("Time's Up");
      clearInterval(myinterval);
    }
  }, 1000);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda