Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

113
Visualizações
React clearInterval doesn't stop when using another state

I have a small app that is playing some audios when I press play and stops when I press the button again using isPlaying state. The functionality of play/stop works good, but I want to add a progress bar to show the song's progress. I used useEffect and a "completed" state so that the progress bar will show the progress with css styling. everything works, except that I can't clear the interval. When I press stop, the song stops, but the progress bar continues, after checking I figured that clearInterval doesn't work. This is my useEffect function:

  const ProgressBar = ({isPlaying}) => {
        const [completed, setCompleted] = useState({
            count: 0
        });
        
         useEffect(() => {
        if (isPlaying){
             setInterval(() => {
                setCompleted(completed.count+=5.8)
                if(completed.count > 99) {
                    setCompleted(completed.count=0);
                } 
            }, 1000);
        }
        else {
            clearInterval()
            
        }
    }, [isPlaying]);

I tried to add:

const intervalId = setInterval(() => { ...

and then pass the intervalId to clear(intervalId) but then I need to add condition of - if (isPlaying) before that. and when I do so, I can't use "else { clearInterval(intervalId)} Because IntervalId is undefined under the else scope. How can I pass that and clear the interval? I tried multiple ways which didn't work for some reason. Any help would be appreciated!

Edited:

I tried to console log just to make sure the playing state get the change from the play/stop toggle, and it works ok:

    useEffect(() => {
    if (isPlaying) {
        console.log("playing")
    }
    else {
        console.log("not playing")
    }

I have "ControlPanel" component, so I can't configure the ProgressBar component functionality with the buttons functionality:

        const ControlPanel = ({
setIsLooping, isLooping, isPlaying, setIsPlaying}) => {
      return ( 
        <div> 
        <PlayButton>
            {!isPlaying ? (
            <button type="button" 
            className="play" 
            onClick={() => setIsPlaying(true)}> 
            <ImPlay2></ImPlay2> Play 
            </button>
            ) : (
            <button type="button"
            className="pause"
            onClick={() => setIsPlaying(false)}> 
            <ImStop></ImStop> Stop 
            </button> 
            )}
        </PlayButton>
            <Flex> .....
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

use can store setInterval id in ref

const intervalId = useRef(null)

useEffect(() => {
    if (isPlaying){
        intervalId.current = setInterval(() => {
            setCompleted(completed.count+=5.8)
            if(completed.count > 99) {
                setCompleted(completed.count=0);
            } 
        }, 1000);
    }
    else {
        clearInterval(intervalId.current)
    }

    // it's better to clean up your component
    return () => {
       clearInterval(intervalId.current)
    }

}, [isPlaying]);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda