AI game generator · Round lifecycle
Why Does My Game Timer Keep Running After Game Over?
Showing a result screen does not automatically stop the clock behind it. Make the end of the round an explicit state change.
Freeze the result when the round ends
When your AI-generated game reaches Game Over, save its elapsed time once and mark the round finished. Render that saved value afterward. Clear any interval owned by the round, and reject late callbacks from an earlier round when restarting.
If the number changes while you read the results, check the value feeding the timer label before asking your AI game generator to rewrite the game. A hidden label, an open results panel and a finished round are three different things.
Find which clock is still changing
There are two common implementations to inspect. An interval may keep incrementing a counter after the end screen appears. Alternatively, a render loop may keep calculating current time minus the start timestamp. Clearing an interval only addresses the first implementation; the second also needs a terminal state and a saved final duration.
MDN documents that clearInterval cancels the repeating action identified by its interval ID. Keep the actual returned ID rather than guessing it. If an engine owns the world update, use its documented lifecycle instead of stopping unrelated rendering, menus or result-screen animations.
A small round ledger makes the defect visible
We ran a synthetic Node clock model with supplied timestamps. It does not wait for real time or test a browser, game engine or commercial generator. The comparison isolates one question: which duration should a completed round return when something reads it later?
The first round starts at zero milliseconds and finishes at 12,000 milliseconds. At 20,000 milliseconds, the unguarded expression still returns 20,000 minus zero: twenty seconds. The state-aware clock returns the twelve seconds saved at the finish event. Eight seconds spent on the results screen have not become extra playing time.
| Model event | Expected duration | Reason |
|---|---|---|
| Round 1 finishes at 12 s | 12 s | Save the terminal result once |
| Read results at 20 s | 12 s | Results-screen waiting is excluded |
| Restart at 30 s | 0 s | New round, new timing origin |
| Read new round at 34 s | 4 s | Measure from its own start |
| Old round callback at 35 s | Ignored | It cannot end the new round |
The distinction matters on Restart as well. Setting the displayed number to zero is insufficient if the next update immediately subtracts the original program start. Our second round starts at 30,000 milliseconds; reading at 34,000 returns four seconds. A delayed finish event carrying the first round identifier is rejected at 35,000. The second round can then finish normally at 39,000, preserving nine seconds even when inspected at 90,000.
Separate active time from the final result
Use three small responsibilities: start establishes the timing origin, elapsed returns the appropriate value for the current phase, and finish freezes the first valid terminal result. The following sketch uses supplied timestamps so the state transitions are explicit. In a browser, use one consistent elapsed-time clock such as performance.now throughout that round; do not mix epoch timestamps and performance timestamps.
let phase = 'idle', round = 0;
let startedAt = 0, resultMs = 0;
// Create a fresh round and invalidate previous callbacks.
function restart(now) {
round += 1;
startedAt = now;
resultMs = 0;
phase = 'playing';
return round;
}
// Keep results stable while the results screen remains open.
function elapsed(now) {
return phase === 'playing' ? now - startedAt : resultMs;
}
// Freeze only the first valid ending for the current round.
function finish(now, eventRound) {
if (eventRound !== round || phase !== 'playing') return;
resultMs = elapsed(now);
phase = 'finished';
}
If a timeout, animation or request will finish the round later, capture its round identifier when scheduling it. Passing whatever identifier is current when the callback finally runs would defeat the guard. If your game has no deferred callbacks, do not add an elaborate cancellation system just to follow this example.
Retain the render loop if the result screen needs it. Change the value that it reads: the finished phase returns resultMs. If you also own a repeating timer, clear its saved interval ID during finish and replace it only when the next round starts. A cancellation call and a state guard solve different problems; neither should substitute for the other.
Our assertions checked frozen results after waiting, a repeated finish event, zero at restart, elapsed time in the new round, rejection of the old round event and the second frozen result. These checks demonstrate the model contract only. Run equivalent actions on your own playable build before calling its timer fixed.
Watch the illustrated explanation of this same round-clock model.
Give the generator a bounded repair request
Keep the existing controls, scoring and results layout. Freeze elapsed time when the current round first enters its terminal state. Render that saved result while finished. On Restart, establish a fresh timing origin and prevent callbacks from the previous round from ending this one. Clear only timers owned by this round. Show the changed lifecycle code and a start, finish, wait, restart check.
Questions to settle before publishing
Should I stop the entire game loop?
Only if your architecture requires it. A results screen may still animate or accept input. Stop gameplay-time accumulation and freeze the final result; keep unrelated interface work functioning.
Is switching browser tabs the same problem?
No. A hidden but active round needs an explicit tab pause or deadline policy. This article concerns a round that has ended. Decide those rules separately.
What if the timer speeds up after each restart?
Check for stacked update loops after Restart. That scheduling problem differs from displaying an unfrozen final duration. For a small repair, follow the one-bug repair guide and compare the same actions before and after.
Sources and boundaries
The interval cancellation behavior comes from MDN. Its performance.now documentation explains a monotonic timestamp relative to the performance time origin. The clock ledger and code above are our isolated state model. They do not establish any generator's behavior, timing precision or browser compatibility.