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

276
Visualizações
setTimeout() doesn't register event target value

I have the following code:

const handleInputChange = (evt) => {
    evt.preventDefault();
    setTimeout(() => {console.log(evt)}, 500);
}

I have a search bar and every time I type something into the handleInputChange function gets called. When looking at the console I find that the evt.target.value field is always set to an empty string regardless of any inputs I have made.

However, if I modify the code to be

const handleInputChange = (evt) => {
    evt.preventDefault();
    const term = evt.target.value;
    setTimeout(() => {console.log(term)}, 500);
}

My target value always gets printed correctly. Can anyone help clarify on why this happens?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When you pass the full event object you and you print the value you are getting the current object status because it is a reference to the original object, so if you clear de input or the event is destroyed your data is not available.

When you save the current event target value you do not care about event status so it is better to save your value instead propagate the event reference through functions.

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