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

217
Views
¿Cómo detener setInterval cuando la pestaña no está activa mientras se usa useEffect?

Estoy usando setInterval en useEffect . Cuando no uso activamente la pestaña, no lo solicito para siempre. Está causando algún problema de memoria. Tengo que buscar datos cada 3000ms , también detenerlo cuando el usuario no esté usando esta pestaña activamente. ¿Cómo puedo hacer tal cosa?

Traté de usar document.visibiltyState y no pude hacerlo.

Mi código:

 useEffect(() => { try { const interval = setInterval(() => { getTransactionGroupStats() getTransactionGroups() }, 3000) getBinanceBalanceStats() return () => { clearInterval(interval) } } catch (error) { console.error(error) } }, [])
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Otra alternativa y un poco más escalable podría ser que crees un gancho personalizado para ver si el usuario está activo o no y cada vez que cambia ejecutas el useEffect

useActive.ts

 export const useActive = (time: number) => { const [active, setActive] = useState(false) const timer: any = useRef() const events = ['keypress', 'mousemove', 'touchmove', 'click', 'scroll'] useEffect(() => { const handleEvent = () => { setActive(true) if (timer.current) { window.clearTimeout(timer.current) } timer.current = window.setTimeout(() => { setActive(false) }, time) } events.forEach((event: string) => document.addEventListener(event, handleEvent)) return () => { events.forEach((event: string) => document.removeEventListener(event, handleEvent)) } }, [time]) return active }

YourApp.tsx

 const active = useActive(3000) useEffect(() => { if(active){ try { const interval = setInterval(() => { getTransactionGroupStats() getTransactionGroups() }, 3000) getBinanceBalanceStats() return () => { clearInterval(interval) } } catch (error) { console.error(error) } } }, [active])
about 4 years ago · Juan Pablo Isaza Report

0

Lo resolví con este enfoque: https://blog.sethcorker.com/harnessing-the-page-visibility-api-with-react/

 export function usePageVisibility () { const [isVisible, setIsVisible] = useState(!document.hidden) const onVisibilityChange = () => setIsVisible(!document.hidden) React.useEffect(() => { document.addEventListener('visibilitychange', onVisibilityChange, false) return () => { document.removeEventListener('visibilitychange', onVisibilityChange) } }) return isVisible }
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!