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

132
Visualizações
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 Respostas
Responde à pergunta

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 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