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

133
Vistas
clearTimeout inside an Event Handler

I'm detecting a scroll end with clearTimeout.

   const Component= ({container}) => {
      const timeoutRef = useRef<number>(0)
    
      const handleScroll = {
         clearTimeout(timeoutRef.current)
         timeoutRef.current = setTimeout(function () {
            if(!timeoutRef.current){ // 'no scroll in progress, do something' }
         }, 100)
      }

    useEffect(() => {
        container.addEventListener('scroll', handleScroll, true)
    }, [])
    
}

But the timeoutRef is not reset when scrolling again, instead, its value only increases. How to fix that?

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

0

That is how setTimeout works. Every time you generate a timeout it generates a new identifier. You use that identifier to cancel the timeout. The timeout does not clear the variable.

If the setTimeout fires, that means it was not cancelled. In your case that means the user did stop scrolling.

 timeoutRef.current = setTimeout(function () {
    console.log('User is done scrolling');
 }, 100)
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