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

264
Views
Javascript setInterval functions passing control to each other

I'm making a game in JS and I'm fairly new to it all. I have created a main loop inside a setInterval, like this:

int = setInterval(() => {
   Do game stuff, keyboard input, update sprite positions, etc...
   Update the screen
},FPS_interval);

But I want this function to be able to pass control to another function that is also in a setInterval. So when I pass I want to stop the interval trigger on this main function and call the other function on a setInterval. When that function has finished I want to come back to this function, again on a setInterval. I'm trying things like this:

int = setInterval(() => {
   Do game stuff, keyboard input, update sprite positions, etc...
   If you die, go to game over screen gameOver();
   Update the screen
},FPS_interval);

function gameOver(){
   clearInterval(int);
   int = setInterval(() => {
      Do game over stuff
      when done, return to main loop.
   }, FPS_interval);
}

One issue I have is when the main loop calls the gameOver function, although the gameOver function clears the interval, the main loop continues to execute to the bottom.

Another way of looking at this is I just want to be able to pass between functions, some of them with separate loops, and keep the screen updating at regular intervals.

Is there a cleaner way? I'm sure there must be!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Probably you're looking for a semaphore variable. Try to declare one and once you get in the gameover set that semaphore and in the main set_interval execute istructions after the gameover call only if the semaphore is not setted.

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!