How can I allow the players of a game to leave their browser (i.e. they are in another tab) but the game is continuously running? I am STILL making my multiplayer game (its going very well) but this is one of the three key points I need to fix. After these issues are fixed the game will be more than halfway done!
A JavaScript Worker still fires setInterval even tho you left the tab or minimized the browser (at least I just tested that with Chrome), which you probably need to send/receive updates from every client to calculate e.g. their ping on the server.
Two simple files to test it out:
File: test.html:
<script>
worker = new Worker('worker.js');
</script>
File: worker.js:
var start = Date.now();
console.log("Worker start", start);
setInterval(()=>{
var end = Date.now();
var diff = end - start;
if (diff > 120) {
console.log('diff', diff);
}
start = Date.now();
}, 100);
The worker barely not reaches the setInterval timeout, but sometimes it goes over some milliseconds:
And here I just pasted the JavaScript code into DevTools, to run it into a normal environment:
The times go crazy and way over 100ms e.g.
What part of your game requires this, gameloop and networking?