I'm trying to make a web-based game and I use setInterval
to update the game (positions, collision checks, etc...) 60 times a second.
It works fine in most browsers, but for some reason Firefox sometimes throttles it to about 35 times a second. Yes, sometimes. If DevTools is closed, it will throttle, but if you open DevTools and start recording performance, it jumps to 60 and will stay that way until DevTools is closed.
If you're using Firefox, run the snippet and try it:
var cycles = 0;
setInterval(()=>{
cycles++;
}, 1000/60);
setInterval(()=>{
console.log("Update cycles per second: %i", cycles);
cycles = 0;
}, 1000);
Is this a bug? If not, why does this happen and how can I work around it?
Edit: I'm not using setInterval
to render. For that I use requestAnimationFrame
. I'm using setInterval
to update the game at a constant time interval, because requestAnimationFrame
fires before repainting, and is heavily dependent on the refresh rate of the user's screen.