fix: interpret Choice.timeLimit as seconds, convert to ms internally

Previously maxLimit was passed directly to setTimeout/setInterval (ms),
so a JSON value of 10 meant 10ms instead of the intended 10 seconds.
Now timeLimit in JSON represents seconds, engine multiplies by 1000.
This commit is contained in:
2026-06-07 18:56:30 +08:00
parent 25ea9ce9fd
commit c7d035bd8f

View File

@@ -23,11 +23,10 @@ export class ChoiceSystem {
const timed = choices.filter((c) => c.timeLimit && c.timeLimit > 0)
if (timed.length === 0) return
const maxLimit = Math.max(...timed.map((c) => c.timeLimit!))
const maxLimitSec = Math.max(...timed.map((c) => c.timeLimit!))
const maxLimitMs = maxLimitSec * 1000
const maxLimitSec = maxLimit / 1000
this.timeLimit = maxLimit
this.timeLimit = maxLimitMs
this.elapsed = 0
this.onUpdate = onUpdate
this.onTimeout = onTimeout
@@ -36,21 +35,19 @@ export class ChoiceSystem {
this.timerId = setInterval(() => {
this.elapsed += this.tickMs
const remaining = Math.max(0, this.timeLimit - this.elapsed) / 1000
const nextState: ChoiceTimerState = {
total: this.timeLimit / 1000,
remaining: Math.ceil(remaining * 10) / 10,
}
this.onUpdate?.(nextState)
const remainingSec = Math.max(0, maxLimitMs - this.elapsed) / 1000
this.onUpdate?.({
total: maxLimitSec,
remaining: Math.ceil(remainingSec * 10) / 10,
})
}, this.tickMs)
this.timeoutId = setTimeout(() => {
this.clear()
// Pick the first choice as default on timeout
if (choices.length > 0) {
this.onTimeout?.(choices[0])
}
}, this.timeLimit)
}, maxLimitMs)
}
stop() {