Here's the scenario: You have a one-window application (a game with spinning reels) that functions well as long as the browser tab it's running in doesn't lose focus. However, when you minimize the browser window or switch to another tab and then return, the app stops working. This issue occurs intermittently, with the app stopping at various intervals during the rotation cycle. The animations are created using requestAnimationFrame(). How can this problem be resolved?
var lastTime = Date.now();
function main() {
var now = Date.now();
var dt = Math.min((now - lastTime), 20) / 1000.0;
update(dt);
render();
lastTime = now;
requestAnimationFrame(main);
}