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

239
Visualizações
cannot clear Interval within if query

I am learning about the setInterval and clearInterval method and meanwhile there appeared some problems with my learning process. In the code below you can see the function countdown which is including the var y which should be increased after every 500 milliseconds. If the var y reaches the value 3 the Interval should be cleared. But my code is not working like I actually want. What I am doing wrong?

  let y = 1
function countdown() {
    
    y += 0.5
    console.log(y)
}

const timerId = setInterval(countdown, 500)

if( y > 3) {
    clearInterval(timerId);
}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to put the ( y > 3) check within the countdown function. Currently it'll only be executed once, and at the end.

Here's a working example:

let y = 1;

const countdown = () => {
  y += 0.5;
  console.log(y);
  if( y >= 3) {
    clearInterval(timerId);
  }
}

const timerId = setInterval(countdown, 500);

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