Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Why will the if condition be executed in this recursive snake game?

shivajikobardan

Legendary Coder
I'm currently watching tutorials to build projects as I'm still not in a phase where I can carve a project that I want all on my own.
Currently, working on a snake game.

JavaScript:
let speed = 2;
let lastPaintTime = 0;

//Game functions
function main(ctime) {
  window.requestAnimationFrame(main);
  if ((ctime - lastPaintTime) / 1000 < 1 / speed) {
    return;
  }
  lastPaintTime = ctime;
  gameEngine();
}

//Main logic starts here
window.requestAnimationFrame(main);

My confusion:
"If condition" should never be checked on this code. Because:
1) window.requestAnimationFrame(main): It calls main function.
2) At the very first line of main function, it again calls main function. So the control should go to main function and forever it should keep calling itself.
3) The if condition should never be checked.





But I asked chatGPT, and it says that if condition will be executed.

PX8zziOdy6Q73OHEZVHpZI11cIyDZniWk566LIsQO11tVEPfv10R4iicE690tlJRIDBih_Ysfcs03ecXt43W0fGRD0rgmitjBbb_9KGTOaG84qbFdo1tG0FRFTqc9CBS3r7_SULUwKFl_aBu7u7uLk-nvmVgcxflrYZmi031tey8ZeRqXdFUGdNZiFRxIg


It says that it doesn't immediately call the main function but schedule/queue it.

What's this behavior called in Javascript language? Where can I read more about it. Is this common for every programming language?

Things I've read from chatGPT

requestAnimationFrame is a method that schedules a function to be called before the next repaint of the browser window. It does not immediately call the function, but rather adds it to a queue of functions to be called at a later time. This allows the browser to update the screen at a consistent frame rate, while also allowing other tasks to be performed in between frames.

When window.requestAnimationFrame(main) runs inside the main function, it calls web api which runs outside of main thread and places the callback in queue and then result is obtained, meanwhile parallely other code (code below "window.requestAnimationFrame(main)" are running and hence the "if" parts gets executed.
 
The loop causes problems. If commented out the variables change values so I changed the loop content to return main, and it worked the same way it did when commented out - by that, I watched the variables change and insured the loop worked.
Know nothing about the gameEngine function so don't know if this paints to the screen.
 
Back
Top Bottom