requestAnimationFrame at beginning or end of function?
javascript, requestanimationframe
Solution
`requestAnimationFrame` does always call its callback asynchronously, so as long as your rendering code is synchronous and does not throw exceptions it doesn't make any difference.
It's essentially a style choice, choose yourself which approach is cleaner. Putting it at the top may emphasise that `render` is scheduling itself, and does so even in the presence of errors in the rendering. Putting it in the bottom allows to conditionally break out of the rendering loop (e.g. when you want to pause your game).
Problem
If I have a loop using requestAnimationFrame like this: ``` function render() { // Rendering code requestAnimationFrame(render); } ``` Will there be any difference if I put the `requestAnimationFrame` in the beginning of the function, like this: ``` function render() { requestAnimationFrame(render); // Rendering code } ``` I haven't noticed any difference, but I have seen both implementations, is one of them better in any way, or are they the same? Edit: One thing I have thought about is, if I put it in the beginning, and the render code takes quite long time to run, say 10ms, wouldn't putting it in the end make the frame rate drop with 10ms?