Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
ujia
/
uj-store
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit e87fc91a
authored
Oct 12, 2026
by
liimin
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
fix(Login): 修复切后台后时钟与日期停滞,可见性恢复时重新锚定时间
1 parent
75e62601
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
39 additions
and
5 deletions
.workbuddy/memory/2026-10-12.md
src/pages/Login/components/LoginAside.tsx
src/pages/Login/components/LoginClock.tsx
.workbuddy/memory/2026-10-12.md
0 → 100644
View file @
e87fc91
## 登录页时钟/日期 可见性监听修复(08:28)
-
问题:切后台后浏览器节流/暂停 setInterval,几天后切回,时钟时间 + 星期 + LoginAside 的日期/问候语仍停在切后台那一刻。
-
LoginClock.tsx:原 today/base 仅挂载时算一次、tick 靠 setInterval 累加。新增 document.visibilitychange 监听,页面重新可见时 setToday(new Date()) + setBase(nowTotalSeconds()) + setTick(0) 重新锚定到当前时间;base/today 改为可更新 state。
-
LoginAside.tsx:now 改为可更新 state;新增可见性监听(切回即 setNow(new Date()) 重算 greet/dateText)+ 60s 定时(防前台常开跨午夜变旧)。
-
tsc --noEmit 通过(EXIT=0)。
src/pages/Login/components/LoginAside.tsx
View file @
e87fc91
...
@@ -6,7 +6,7 @@
...
@@ -6,7 +6,7 @@
* @FilePath: \uj-store\src\pages\Login\components\LoginAside.tsx
* @FilePath: \uj-store\src\pages\Login\components\LoginAside.tsx
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
*/
import { useState } from 'react';
import { use
Effect, use
State } from 'react';
import { Logo } from '@/components/Logo';
import { Logo } from '@/components/Logo';
import { LoginClock } from './LoginClock';
import { LoginClock } from './LoginClock';
...
@@ -25,10 +25,24 @@ function greetOf(hour: number): string {
...
@@ -25,10 +25,24 @@ function greetOf(hour: number): string {
}
}
export function LoginAside() {
export function LoginAside() {
const [now] = useState(() => new Date());
const [now
, setNow
] = useState(() => new Date());
const greet = greetOf(now.getHours());
const greet = greetOf(now.getHours());
const dateText = `${now.getFullYear()}年${now.getMonth() + 1}月${now.getDate()}日`;
const dateText = `${now.getFullYear()}年${now.getMonth() + 1}月${now.getDate()}日`;
useEffect(() => {
// 每 60s 刷新一次,保证前台常开跨午夜时日期/问候语不陈旧。
const timer = window.setInterval(() => setNow(new Date()), 60_000);
// 切后台再切回:重新可见时立即按当前时间重算,避免停在切后台那一刻。
const onVisible = () => {
if (document.visibilityState === 'visible') setNow(new Date());
};
document.addEventListener('visibilitychange', onVisible);
return () => {
window.clearInterval(timer);
document.removeEventListener('visibilitychange', onVisible);
};
}, []);
return (
return (
<aside className="uj-login-aside">
<aside className="uj-login-aside">
<div className="uj-login-brand">
<div className="uj-login-brand">
...
...
src/pages/Login/components/LoginClock.tsx
View file @
e87fc91
...
@@ -25,15 +25,29 @@ const pad = (n: number) => String(n).padStart(2, '0');
...
@@ -25,15 +25,29 @@ const pad = (n: number) => String(n).padStart(2, '0');
const handStyle = (deg: number): CSSProperties => ({ transform: `rotate(${deg}deg)` });
const handStyle = (deg: number): CSSProperties => ({ transform: `rotate(${deg}deg)` });
export function LoginClock() {
export function LoginClock() {
const [today] = useState(() => new Date());
const [today
, setToday
] = useState(() => new Date());
// 基准秒(当日已过秒数)+ 单调递增的 tick。指针角度由二者之和推导且不取模 360:
// 基准秒(当日已过秒数)+ 单调递增的 tick。指针角度由二者之和推导且不取模 360:
// 一旦取模,跨点时 CSS 要从 rotate(354deg) 插值到 rotate(0deg),回弹动画会整圈倒转。
// 一旦取模,跨点时 CSS 要从 rotate(354deg) 插值到 rotate(0deg),回弹动画会整圈倒转。
const [base] = useState(nowTotalSeconds);
const [base
, setBase
] = useState(nowTotalSeconds);
const [tick, setTick] = useState(0);
const [tick, setTick] = useState(0);
useEffect(() => {
useEffect(() => {
const timer = window.setInterval(() => setTick((t) => t + 1), 1000);
const timer = window.setInterval(() => setTick((t) => t + 1), 1000);
return () => window.clearInterval(timer);
// 切后台再切回:后台期间 setInterval 被浏览器节流/暂停,tick 停滞,
// 回来后时间停在「切后台那一刻」。监听页面可见性,重新可见时按当前时间重新锚定。
const onVisible = () => {
if (document.visibilityState === 'visible') {
const d = new Date();
setToday(d);
setBase(nowTotalSeconds());
setTick(0);
}
};
document.addEventListener('visibilitychange', onVisible);
return () => {
window.clearInterval(timer);
document.removeEventListener('visibilitychange', onVisible);
};
}, []);
}, []);
const total = base + tick;
const total = base + tick;
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment