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.
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:
window.requestAnimationFrame(main): It calls main function.
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.
The if condition should never be checked.
But I asked chatGPT, and it says that if condition will be executed.
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.
First, stop using chatGPT until you have learned the basics of JavaScript. Start with a Basic JavaScript course like ours to get a good sense of the basics. Trying to take shortcuts with chatGPT will cause you to waste time because you do not understand how JavaScript works. You can not shortcut the learning process until they come up with a chip you can insert into your brain and load a program into like in The Matrix.
Second, the behavior is called recursion. You will learn about it in our curriculum.
requestAnimationFrame takes a callback function. How/when it is called, depends on the API of the higher-order function (i.e. the function/method that takes the callback).
In the case of requestAnimationFrame I believe the “event” that drives it is timing based.
The function to call when it’s time to update your animation for the next repaint. The callback function is passed one single argument, a DOMHighResTimeStamp similar to the one returned by performance.now(), indicating the point in time when requestAnimationFrame() starts to execute callback functions.
For a more broad look into scheduling/queuing by the engine/runtime.
By definition, rAF waits till next repaint of window which is 16ms for 60fps screen. It’s like setTimeOut but better.
window.rAF() is executed, so it is removed from the call stack. Call stack is empty. Meanwhile at the same time, after 16ms has been passed, main() has been put into callback queue.
Now here is where the event loop comes in. The event loop is a continuous running process that constantly checks if the call stack is empty or not. If the call stack is empty, it will move the function from the callback queue into the call stack and it gets executed. So, main() gets executed.
Again, it calls window.rAF(main).
window.rAF() has now executed.
Now, the code under window.rAF() runs(the if condition and so on and so forth). After, 16ms, main() is passed to callback queue.