Is there a simple way to make a game loop in JavaScript? something like...
onTimerTick() {
// update game state
}
Is there a simple way to make a game loop in JavaScript? something like...
onTimerTick() {
// update game state
}
setInterval(onTimerTick, 33); // 33 milliseconds = ~ 30 frames per sec
function onTimerTick() {
// Do stuff.
}
Yep. You want setInterval
:
function myMainLoop () {
// do stuff...
}
setInterval(myMainLoop, 30);
Would this do?
setInterval(updateGameState, 1000 / 25);
Where 25 is your desired FPS. You could also put there the amount of milliseconds between frames, which at 25 fps would be 40ms (1000 / 25 = 40).