Here is the magic part of the code, checking if enough time has passed:
Code: Select all
var fpsInterval = 1000 / 5; // Giving 5 fps.
var Now, ElapsedTime, LastDraw;
function GameLoop()
{
Now = Date.now();
ElapsedTime = Now - LastDraw;
if (ElapsedTime >= fpsInterval)
{
// Enough time has passed, time to draw.
LastDraw = Now;
// Do your drawing here.
}
window.requestAnimationFrame(GameLoop);
}