Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
¿Cómo puedo borrar el intervalo en React?

Estoy usando setInterval en reaccionar redux. A continuación se muestra mi función.

FileAction.js

 export const SetPath = ({ path, location }) => async (dispatch) => { try { let interval; if (pre_path === path) { interval = setInterval(() => { handler(path); }, 3000); } else { pre_path = path; clearInterval(interval); } const handler = async (pth) => { const mtime = await axios.post('/api/file', { data: pth }); dispatch({ type: GET_EVENT, payload: { time: mtime, position: location } }); } } catch (err) { dispatch({ type: GET_ERROR }); } };

Lo que me gustaría hacer es cuando llamo a la función SetPath , tiene que obtener datos de api/file cada 3 segundos.

Pero ahora, cuando paso otra ruta a la función SetPath , duplica el intervalo.

¿Cómo puedo implementar esto?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Está definiendo la variable de interval dentro de la función setPath , por lo tanto, la variable se reinicializará cada vez que se ejecute setPath . Intenta definirlo fuera de la función. Es posible que deba usar useRef para no perder la variable cuando se produzca una nueva representación.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!