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

177
Vistas
Is it possible to stop a loop with another function in Javascript?

I have a function that is continuously incrementing a value. I am trying to implement a reset button that would reset the value to 0 and prevent it from incrementing any more. However, the reset button only changes the value but the loop keeps on going. Here is the code that accomplishes that (the actual value that is increasing is not mentioned in this block, the counter variable is involved with the rate at which the value is increased):

var counter = 0;

function create() {
//creates stuff
}

var timer;


function loop() {
        create()
}

function counter() {
    clearInterval(timer)
    timer = setInterval(loop, 1000 / (2**counter))
}

And it works as expected. However, my reset button looks like this:

function reset() {
    counter = 0
    clearInterval(timer)
    timer = setInterval(loop, 0)

The counter just continues to increase at the same rate it was before the reset. Is it possible to terminate this process?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Can you try below?

function initCounter() { 
  clearInterval(timer);
  timer = setInterval(loop, 1000 / (2**counter));
} 

function reset() { 
  counter = 0;
  initCounter(); 
}

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