From ddfcd14252da644affc28e787c467971882c4e5e Mon Sep 17 00:00:00 2001 From: cnliucheng Date: Wed, 8 Jul 2026 14:09:35 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20timer=20=E5=90=8E=E5=8F=B0=E5=9B=9E?= =?UTF-8?q?=E5=88=B0=E5=89=8D=E5=8F=B0=E6=97=B6=E7=AB=8B=E5=8D=B3=E5=88=B7?= =?UTF-8?q?=E6=96=B0=20UI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit WeChat 在后台冻结 setInterval,用户从训练页切走再回来时 看到的还是离开前的画面。注册 wx.onAppShow 监听器,回到 前台立即强制 _tick() 一次,UI 跳到真实 elapsed,半程/30s/10s 等延迟到达的提醒也能补触发。 --- utils/timer.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/utils/timer.js b/utils/timer.js index c0d1c41..312d2c6 100644 --- a/utils/timer.js +++ b/utils/timer.js @@ -12,6 +12,17 @@ class Timer { this._running = false this._paused = false this._completed = false + this._appShowBound = false + + // WeChat freezes setInterval while the mini-program is in background. + // When the user returns, force an immediate tick so the UI jumps to + // the actual elapsed time (instead of staying on the last pre-background + // frame) and any missed halfway / 30s / 10s reminders can still fire. + this._onAppShow = () => { + if (this._running && !this._paused) { + this._tick() + } + } } start(duration) { @@ -24,6 +35,15 @@ class Timer { this._startTime = Date.now() this._tick() this._intervalId = setInterval(() => { this._tick() }, 1000) + + // Register foreground listener (idempotent — we keep a flag so we don't + // double-register if start() is called multiple times in one session) + if (!this._appShowBound && typeof wx.onAppShow === 'function') { + try { + wx.onAppShow(this._onAppShow) + this._appShowBound = true + } catch (e) {} + } } pause() {