Collisions not working when browser tab changes
Closed this issue · 2 comments
yojona commented
GrosSacASac commented
It probably has to do with the fact that browser slow down setInterval and requestAnimationFrame and others for inactive tabs.
I recommend to pause the game when the tab is inactive.
document.addEventListener(`visibilitychange`, function () {
if (document.hidden) {
pause();
}
});
yojona commented
In case someone else have the same problem:
if I doa simple if/else the result is the same, I had to add a delay of 100ms before resuming the game again to avoid this problem.
thanks @GrosSacASac
const delay = ms => new Promise(res => setTimeout(res, ms));
document.addEventListener('visibilitychange', async () => {
if (document.hidden) {
setPause(true);
} else {
await delay(100)
setPause(false)
}
});