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

264
Visualizações
clearInterval() doesn't clear interval in React

I want to increment the number of users after each 200ms till 5000 with the below code. But it doesn't clear the interval when the number of users greater than 5000.

const Cards = () => {
const [users, setUsers] = useState(40);

useEffect(() => {
    const setIntervalUsers = setInterval(() => {
        setUsers((prevUsers) => prevUsers = prevUsers + 100)
    }, 200);

    if (users >= 5000) {
        console.log('ok');
        clearInterval(setIntervalUsers)
    }
}, []);


return (<div>number of users {users} </div>)}
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I would suggest you to return a clean up function so you don't register the interval twice in case you are in StrictMode with React 18, and also to remove it from the memory when the component gets unmounted.

Also use a ref sets with useRef and a separate useEffect that would watch changes in users and clear the interval there. Like so:

import { useEffect, useRef, useState } from "react";
const Cards = () => {
  const [users, setUsers] = useState(40);
  const intervalRef = useRef();

  useEffect(() => {
    if (users >= 5000) {
      console.log("ok");
      clearInterval(intervalRef.current);
    }
  }, [users]);

  useEffect(() => {
    intervalRef.current = setInterval(() => {
      setUsers((prevUsers) => (prevUsers = prevUsers + 100));
    }, 200);
    return () => clearInterval(intervalRef.current);
  }, []);

  return <div>number of users {users} </div>;
};
about 4 years ago · Santiago Trujillo Relatório

0

The way how you check for your condition users >= 5000 is not working because users is not listed as a dependency in your useEffect hook. Therefore the hook only runs once but doesnt run again when users change. Because of that you only check for 40 >= 5000 once at the beginning.

An easier way to handle that is without a setInterval way.

export const Cards = () => {
  const [users, setUsers] = useState(40);
  
  useEffect(() => {
    // your break condition
    if (users >= 5000) return;
    
    const increment = async () => {
      // your interval
      await new Promise((resolve) => setTimeout(resolve, 200));
      setUsers((prevState) => prevState + 100);
    }
    
    // call your callback
    increment();
    
    // make the function run when users change.
  }, [users]);
  
  return <p>current number of users {users}</p>
}
about 4 years ago · Santiago Trujillo Relatório

0

This doesnt work because:

  • you never call the useEffect again to check if the condition is met
  • the interval ref is lost

I made a working sample of your code here : https://codepen.io/aSH-uncover/pen/wvmYdNy

Addintionnaly you should clean the interval when the component is destroyed by returning the cleanInterval call in the hook that created the inteerval

const Card = ({ step }) => {
  const intervals = useRef({})
  
  const [users, setUsers] = useState(40)
  
  useEffect(() => {
    intervals.users = setInterval(() => {
      setUsers((prevUsers) => prevUsers = prevUsers + step)
    }, 200)
    return () => clearInterval(intervals.users)
  }, [])
  
  useEffect(() => {
    if (users >= 5000) {
        clearInterval(intervals.users)
    }
  }, [users])
  
  return (<div>number of users {users} </div>)
}
about 4 years ago · Santiago Trujillo 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