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

140
Vistas
Timer stop at 0

I have this working code and I would like the timer to stop at 0 instead of continuing with negative numbers

  function animateValue(id) {
  var obj = document.getElementById(id);
  var current = +localStorage.getItem('counter') || +obj.innerHTML;
  obj.innerHTML = current;
  function decrement(){
    --current;
    obj.innerHTML = current;
    localStorage.setItem('counter', current);
    if(current != 0){
        setTimeout(decrement, (Math.floor(Math.random()*11) + 10) * 1000);
    }
  }
  setTimeout(decrement, (Math.floor(Math.random()*11) + 10) * 2000);
}

animateValue('counter');

i tried to add but to no avail

if (seconds === 0) {
    stop();
}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Wrap the code inside decrement in an if statement like so:

function decrement() {
    if (current > 0) {
        --current;
        obj.innerHTML = current;
        localStorage.setItem('counter', current);
        setTimeout(decrement, (Math.floor(Math.random()*11) + 10) * 1000);
    }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

If you use setInterval for it, you can deactivate it with clearInterval.

It will work at least that I think

about 4 years ago · Juan Pablo Isaza Denunciar

0

For this bit you tried,

   if (seconds === 0) {
    stop();
}

Is this not meant to be

if (current <= 0) {
    stop(); //I assume you have some code to stop it
}
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