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

208
Vistas
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 Respuestas
Responde la pregunta

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 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