2.2 The game loop
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.