Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda