From 67f308e47fc46c8567f45b10aeef58b3d2c6e5ed Mon Sep 17 00:00:00 2001 From: codytseng Date: Mon, 16 Mar 2026 22:08:51 +0800 Subject: [PATCH] fix: stop YouTube mute-check interval when player is destroyed The setInterval polling playerRef.current.isMuted() continued running after the YouTube player was destroyed, causing repeated TypeErrors. Now the interval self-clears when it detects the player is invalid. Co-Authored-By: Claude Opus 4.6 (1M context) --- .../YoutubeEmbeddedPlayer/Player.tsx | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/components/YoutubeEmbeddedPlayer/Player.tsx b/src/components/YoutubeEmbeddedPlayer/Player.tsx index 3c6590a..eef4549 100644 --- a/src/components/YoutubeEmbeddedPlayer/Player.tsx +++ b/src/components/YoutubeEmbeddedPlayer/Player.tsx @@ -55,7 +55,7 @@ const Player = memo(({ videoId, isShort, className }: PlayerProps) => { } }) - let checkMutedInterval: NodeJS.Timeout | null = null + let checkMutedInterval: ReturnType | null = null function initPlayer() { try { if (!videoId || !containerRef.current || !window.YT.Player || unmountedRef.current) return @@ -87,20 +87,29 @@ const Player = memo(({ videoId, isShort, className }: PlayerProps) => { } setInitSuccess(true) checkMutedInterval = setInterval(() => { - if (playerRef.current && !unmountedRef.current) { - const mute = playerRef.current.isMuted() - if (mute !== currentMuteState) { - currentMuteState = mute + if ( + !playerRef.current || + unmountedRef.current || + typeof playerRef.current.isMuted !== 'function' + ) { + if (checkMutedInterval) { + clearInterval(checkMutedInterval) + checkMutedInterval = null + } + return + } + const mute = playerRef.current.isMuted() + if (mute !== currentMuteState) { + currentMuteState = mute - if (mute !== muteStateRef.current) { - updateMuteMedia(currentMuteState) - } - } else if (muteStateRef.current !== mute) { - if (muteStateRef.current) { - playerRef.current.mute() - } else { - playerRef.current.unMute() - } + if (mute !== muteStateRef.current) { + updateMuteMedia(currentMuteState) + } + } else if (muteStateRef.current !== mute) { + if (muteStateRef.current) { + playerRef.current.mute() + } else { + playerRef.current.unMute() } } }, 200)