2.2 The game loop

In notebook:
edX Advanced HTML5
Created at:
2016-01-17
Updated:
2016-01-18
Tags:

setTimeout vs setInterval

setTimeout is slightly better as you can control when the next loop execution occurs. setInterval will start a new loop even if the previous calculation has not yet finished.

requestAnimationFrame API

Much better than the above. Call the game mainloop with ​requestAnimationFrame​ and use the timestamp arguments to calculate elapsed time since the last execution and calculate how many pixels an object needs to move for example.