Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!