Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

211
Vistas
How can I stop setInterval?

I'm trying to clear setInterval with calling clearInterval. however, i call setInterval function inside function. So I'd like to trigger the parameter in the setInterval by clearInterval, But i do not know how to get reach this thing. And what i say is confusing so I just want to show u the code :

function getAPI() {
    const polling = () => {
      if (convert !== undefined) {
        axios
          .post("/realtime", {
            GET_API_URL: convert.GET_API_URL,
            Headers1_key: convert.Headers1_key,
            Headers1_Value: convert.Headers1_Value,
            Headers2_key: convert.Headers2_key,
            Headers2_Value: convert.Headers2_Value,
            Headers3_key: convert.Headers3_key,
            Headers3_Value: convert.Headers3_Value,
          })
          .then(response => {
            setRandom(response.data);
          })
          .catch(error => {
            console.log(error);
          });
      } else {
        alert("try again.");
      }
    };
    setInterval(polling, convert.API_Read_Speed);
  }

  const init = () => {
    clearInterval(polling);  // i want to call polling and i want to stop the axios posting
  };

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

according to your comment seems like you need to stop the polling in react. you can use the useRef hook to keep the reference of the interval and clear it whenever you like.

 useEffect(() => {
  interval.current = setInterval(() => {
    ...
  }, ms)

 }, [input]);

 const closeConnection = () => {
  clearInterval(interval.current);

 }
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can store setInterval inside a variable and access it like that.
Example:

let interval;

function foo() {
   interval = setInterval(() => {
      ...
   }, ms)
}

function bar() {
   clearInterval(interval);
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda