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

132
Vistas
JavaScript ClearInterval button doesn't stop

I've reviewed the other clearInterval posts here for a clue as to why my code isn't functioning as intended. Despite my best efforts, I still can't get clearInterval to stop my count.

I'm trying to do something simple: have a count, increase by one until the stop button is pushed.

I have tried different, and incorrect variables, but I cannot find a working solution.

    <p>Count Up:</p>
    <p id="count">1</p>
    <button onclick="clearInterval(current)">Stop</button>
    <script>
      var current = 1;
      function animateValue(id) {
        var obj = document.getElementById(id);
        var current = parseInt(obj.innerHTML);
        setInterval(function () {
          current++;
          obj.innerHTML = current;
        }, 1000);
      }
      animateValue("count");
    </script>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Store the value returned by setInterval in a variable and pass that as the parameter to clearInterval instead of counter:

<p>Count Up:</p>
<p id="count">1</p>
<button onclick="clearInterval(interval)">Stop</button>
<script>
  var interval;
  var current = 1;
  function animateValue(id) {
    var obj = document.getElementById(id);
    var current = parseInt(obj.innerHTML);
    interval = setInterval(function () {
      current++;
      obj.innerHTML = current;
    }, 1000);
  }
  animateValue("count");
</script>

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