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

224
Visualizações
React.js and Firebase auth: setTimeout Callback function not executed?

I have a function handleSubmit that handles registering in Firebase in a react component. Inside, I want to handle errors with my setErrorTimeout function, which has a setTimeout that resets the error automatically after 3 seconds in this case..

The problem is, my Timeout is not executed, e.g the callback function inside the timeout is not being executed after 3 seconds, but everything else is.. why?

    const handleSubmit = async e => {
    e.preventDefault()

    console.log(formDetails)
 
    if (formDetails.password !== formDetails.passwordrepeat) {
        setErrorTimeout(setRegisterError, {
            message: 'Passwords do not match!',
        })
        return
    }

    console.log('Try')
    console.log(formDetails.email, formDetails.password)
    try {
        auth.createUserWithEmailAndPassword(
            formDetails.email,
            formDetails.password
        )
            .then(userCredentials => {
                if (userCredentials) {
                    const user = userCredentials.user
                    let success = user.sendEmailVerification()
                    console.log('success register:', success)
                    setRegisterSuccess(
                        'You registered successfully! please check your email!'
                    )

                    setFormDetails({})
                }
            })
            .catch(error => {
                console.log('ERROR!')
                setErrorTimeout(error)
            })
    } catch (e) {
        setErrorTimeout(e)
    }
}


const setErrorTimeout = error => {
    console.log('inside timeout!')
    setRegisterError(error)
    const timer = setTimeout(() => {
        console.log('inside cb!')
        setRegisterError(null)
    }, 3000)
    clearTimeout(timer)
    console.log('after timeout!')
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're clearing the timeout right after you create it here:

const timer = setTimeout(() => {
    console.log('inside cb!')
    setRegisterError(null)
}, 3000)
clearTimeout(timer)

You probably want that clearTimeout call to be inside the callback, although it's not even strictly needed since the timeout already fired.

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