4 ms·
Why is code below recursive requestAnimationFrame ever executed?
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:
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. https://imgur.com/a/eDRS8N5
It says that it doesn't immediately call the main function but schedule/queue it.
What's this behaviour 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.
- bob1029 4y ago> 1) window.requestAnimationFrame(main): It calls main function. No. It's not "calling" main. It is scheduling a future invocation of main outside of the current execution context. The return value of that function is a handle that lets you cancel the scheduled invocation, if desired. In terms of concepts, this is related to the event loop and callback mechanisms. Maybe check this article out: https://www.digitalocean.com/community/tutorials/understanding-the-event-loop-callbacks-promises-and-async-await-in-javascript https://www.digitalocean.com/community/tutorials/understandi...
- shivajikobardan 4y agocan someone please build some visualization for me for this? A simple sketch will work.
- beardyw 4y agoThe above explains it. I am not sure a diagram will help. In the simplest of terms, your code will run until it has no more to do. If you set something else running (like request animation frame or fetch from the server) it may be doing something in parallel but the JavaScript code associated with it will only run when its turn comes. Only one part of your code is ever running at a time. You can run code in parallel yourself by creating a new environment called a worker but that is more advanced.
- shivajikobardan 4y agodiagram helps. I don't have time to share it by pasting to imgur here, but you can check this thread. https://forum.kirupa.com/t/why-does-if-condition-executes-in-this-requestanimationframe-code/656074/5 https://forum.kirupa.com/t/why-does-if-condition-executes-in...
- Someone 4y ago> 1) window.requestAnimationFrame(main): It calls main function. No, it doesn’t. It asks the browser to call the function. It will do that at some later time. If you wanted to call main, you’d just write main(). https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame https://developer.mozilla.org/en-US/docs/Web/API/window/requ...: “The window.requestAnimationFrame() method tells the browser that you wish to perform an animation and requests that the browser calls a specified function to update an animation before the next repaint.”
- eesmith 4y ago> 1) window.requestAnimationFrame(main): It calls main function. It doesn't call the main function. It asks the window object to call the main function before the next repaint, which will be at some point in the future. This "main" is an example of a callback function. [EDIT: see bob1029's comment for more details.] Once called, the window object forgets about the callback to the main function, so it needs to be specified again. Again, it isn't called right away, and there is no loop. This bit of logic says the browser should keep calling the main function before each repaint, and for each call it tracks the time since the game engine was last executed. If it's been long enough, it advances the game engine. This keeps the game engine game rate (mostly) coupled from the paint rate. Another code example for requestAnimationFrame is available from https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame https://developer.mozilla.org/en-US/docs/Web/API/window/requ...