2 ms·
> 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
by 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...