AI GAME GENERATOR · PAUSE RULES
Should Your AI Game Pause When You Switch Tabs?
For a local action game, pause when the page becomes hidden and offer a Resume button on return. For a timed event that must keep running, retain a deadline and recalculate time left. Choose the rule explicitly; browser callback timing is not the rule.
A game from an AI game generator can look frozen while its countdown follows a different clock. Decide what the player should experience before asking for a timer fix. A solo puzzle, an idle reward and a shared competitive round need different policies.
What the browser tells you
The Page Visibility API reports whether a document is visible or hidden and emits visibilitychange events. Window focus is a different signal: losing focus does not necessarily mean the page is hidden. MDN also documents background timer throttling. Its requestAnimationFrame reference says callbacks are paused in most browsers in background tabs. Neither behavior specifies your game's pause policy.
A 60-second round, with two possible answers
I ran a small JavaScript model with synthetic timestamps. A round starts at time zero with sixty seconds available. At ten seconds the page is labelled hidden. It becomes visible at forty seconds, but the player waits until forty-five seconds to press Resume. We inspect the result at fifty seconds. The same event list feeds both calculations.
The pause policy counts only active play. Ten seconds pass before hiding, then another five after Resume. Fifteen seconds were used, so forty-five remain. The deadline policy sets the end at sixty seconds after the start. At fifty seconds, ten remain. Neither answer is automatically a bug: they express different promises to the player.
Pause during absence
60 − (10 + 5) = 45 seconds left
Returning does not start play.
Keep a fixed deadline
60 − 50 = 10 seconds left
The interval away still counts.
Check boundaries, not just the final number
The recorded trace includes a second hidden event at the same ten-second timestamp. It adds no active time and cannot toggle the game back into motion. The visible event at forty seconds leaves the pause-policy game paused. That distinction creates a useful return screen: the player can find the controls before play resumes instead of losing immediately on tab return.
A final inspection at sixty-five seconds leaves thirty seconds under the pause policy and zero under the deadline policy. The remaining-time calculation clamps at zero. It does not establish that a real timeout handler ran exactly at the deadline while the page was hidden. On return, the application must reconcile the expired state and apply its end-of-round action once.
Six assertions check the two fifty-second results, the visible-but-paused state, duplicate-hidden handling, and the two sixty-five-second results. The model tests arithmetic and transitions only. It does not switch a real browser tab, lock a phone, suspend an operating system, play audio or contact a competitor. Those are separate tests, not hidden assumptions behind these numbers.
Prevent a large movement jump on Resume
For the local pause policy, reset the animation time baseline when play actually resumes. Suppose the previous animation frame belongs to before the absence. Subtracting that old timestamp from the new one and applying the entire gap as one movement update can create a jump. Discarding paused time is appropriate for this policy; silently discarding time from a shared deadline would change a different rule.
Keep visibility and player pause as separate state. If the player paused before switching tabs, returning should not undo that choice. Clear held movement inputs when hiding, because a key release may occur while another page has focus. Resume one existing loop rather than registering another loop or another input handler on every return. These are repair requirements; this experiment did not execute a rendered movement loop.
A prompt with a result you can check
Work in a copy of my local single-player game. Preserve its movement, scoring and artwork. Pause simulation and its active-play countdown when the document becomes hidden. Clear held inputs. On return, show Resume and keep the round paused until pressed. Preserve a previous manual pause and mute choice. Reset the frame timestamp and resume only one loop. Run a synthetic sixty-second round: hide at ten, return at forty, resume at forty-five, inspect at fifty. Show forty-five seconds left and the transition log. Repeat the hide event to check that it does not resume play. List real-device checks you have not run.
For an event that must expire while the player is away, replace that active-play requirement with an explicit deadline requirement and expect ten seconds at the same inspection point. In a shared competitive game, the server should decide the authoritative deadline and result; a client's editable clock is not a trusted authority. Keep the local toy model separate from that server design.
What still needs a device test
Test an actual tab switch, window minimization, phone app switch and screen lock on the devices you support. Record the chosen policy, time remaining before and after, held-key state and sound behavior. Do not treat one desktop pass as proof of phone behavior. A 2017 Safari Mobile developer question describes different audio behavior for screen locking and ordinary focus changes. That historical report motivates separate checks; it is not evidence about current Safari.
Pause and timer questions
Will requestAnimationFrame pause my entire game?
No. A paused animation callback is not a complete gameplay, countdown, input or sound policy. Define and test each state explicitly.
Should background music restart when I return?
Preserve the player's mute or pause choice. Resume only appropriate audio and handle a browser refusing playback. This guide has not tested audio or phone lock-screen behavior.
What if the page reloads instead of returning?
A reload needs a separate recovery rule. A paused in-memory round does not guarantee saved progress; preserve player progress across updates if persistence is part of the design.
After changing the policy, use the pre-publish game testing checklist on the actual build you will share.