fix: sleep timer countdown (#2233) (#2235)

This commit is contained in:
York
2026-07-16 12:45:10 +08:00
committed by GitHub
parent be841e7ee9
commit 4de9b21ff7
@@ -107,34 +107,39 @@ const useSleepTimer = () => {
const status = usePlayerStatus(); const status = usePlayerStatus();
const handleOnPlayerProgress = useCallback(() => { useEffect(() => {
if (!active) { if (!active || mode !== 'timed' || status !== PlayerStatus.PLAYING) {
return; return undefined;
} }
if (status !== PlayerStatus.PLAYING) { let lastTick = Date.now();
return; const interval = window.setInterval(() => {
} const now = Date.now();
const elapsedSeconds = Math.floor((now - lastTick) / 1000);
// Count down in timed mode if (elapsedSeconds < 1) {
if (mode === 'timed') { return;
}
lastTick += elapsedSeconds * 1000;
const remaining = useSleepTimerStore.getState().remaining; const remaining = useSleepTimerStore.getState().remaining;
if (remaining <= 0) { if (remaining <= elapsedSeconds) {
cancelTimer(); cancelTimer();
mediaPauseRef.current(); mediaPauseRef.current();
} else { } else {
setRemaining(Math.max(0, remaining - 1)); setRemaining(remaining - elapsedSeconds);
} }
} }, 1000);
return () => window.clearInterval(interval);
}, [active, cancelTimer, mode, setRemaining, status]); }, [active, cancelTimer, mode, setRemaining, status]);
usePlayerEvents( usePlayerEvents(
{ {
onCurrentSongChange: handleOnCurrentSongChange, onCurrentSongChange: handleOnCurrentSongChange,
onPlayerProgress: handleOnPlayerProgress,
}, },
[handleOnCurrentSongChange, handleOnPlayerProgress], [handleOnCurrentSongChange],
); );
// End-of-song mode: set the pauseOnNextSongEnd flag so that // End-of-song mode: set the pauseOnNextSongEnd flag so that