JavaScript Game Development
Different Solutions for Game Loops
Simple Example requestAnimationFrame
function animate() {// stuff for animating goes hererequestAnimationFrame(animate);}animate();
requestAnimationFrame
wrapped in a time-out is a clean way of controlling the frame rate for vanilla.js games.
let framesPerSecond = 10;function animate() {setTimeout(function() {requestAnimationFrame(animate);// animating/drawing code goes here}, 1000 / framesPerSecond);}
EXAMPLE
var promise = new Promise(function(resolve, reject){resolve(5);});promise.then(function(data){return data * 2;}).then(function(data){return data + 20;}).then(function(data){console.log(data);});// log 30