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

215
Visualizações
JavaScript setInterval only works once

I hope you are doing well. I'm working on a minigame based on JS and want to prevent the user from shooting infinite bullets, so I created this condition. Which only works for the first time and then breaks for the next clicks

also to mention this whole section is inside the loop so shooting works well with or without setInterval

  let shootController = true

  canvas.addEventListener("click", () => {
    if (shootController) {
      //shooting code goes here
      shootController = false;
    }
  });

  if (shootController === false) {
    setInterval(() => {
      shootController = true;
    }, 1000);
  }

Thanks for your replies beforehand

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

0

Somehow, I defined the 'setInterval' before and outside the loop and then called that function below and outside. This one WORKS

But from curiosity, I didn't define it at the top as a function and only used it below the loop. This one didn't work.

The problem is solved, but I don't know why.

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