One Week Game
Tiny Game Loop for Javascript

It’s time to share some quick tips.

I do a lot of prototypes in javascript and for the most part it’s quicker for me to write this game loop than it is to open a template file.

var FPS = 60;
function loop() {
    setTimeout(function(){loop(),update(),draw()}, 1000/FPS);

function update() {
  // Update code here
function draw() {
  // Draw code here

loop(); // Start loop

I prefer the single line version myself, but that’s because I’m a bad person.

function loop(){(function(){setTimeout(function(){loop(),update(),draw()}, 1000/FPS);})();}

I should also point out that you should be using requestAnimationFrame for most browser games, not setTimeout. I use setTimeout just for better control of the FPS. (requestAnimationFrame tries to run at 60fps)

  1. oneweekgame posted this