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

200
Visualizações
Calling a function based on input field value changes in JavaScript

Somehow my same closed ,because the question was not clear to everyone. I a repeating the same question. Here i have this input field when the user is typing I am calling handleMessagePress(e) function continuously ,and when the user stops typing I, am calling stop() function after five seconds . But my question is when the user starts typing , i need to call handleMessagePress(e) function only once and wait for 5 seconds and if the user is still typing the same function should be called automatically otherwise the stop function should be called automatically. In simpler words after 5seconds my Program should be able to check user is typing or not whether the user is typing or not if typing call handleMessagePress(e) otherwise call stop function automatically

 <input placeholder="type your message" id="messageInputField"  onKeyPress={(e) => handleMessagePress(e)}/>



  let myInput = document.getElementById("messageInputField");
  let timer;
  if (myInput) {
    myInput.addEventListener("keyup", () => {
      clearTimeout(timer);
      timer = setTimeout(stop, 5000);
    });
  }
 
const stop=()=>{
console.log("user stop typing");
}

 const handleMessagePress = (e) => {
    console.log("user is typing");
    }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

 let myInput = document.getElementById("messageInputField");
  let timer;
  let executeHandlePress = true; // flag to watch handle press function execution
  if (myInput) {
    myInput.addEventListener("keyup", () => {
      clearTimeout(timer);
      timer = setTimeout(stop, 5000);
    });
  }
 
const stop=()=>{
console.log("user stop typing");
}

 const handleMessagePress = (e) => {
    if(!executeHandlePress){ // if flag false, return
    return;
    }
     console.log("user is typing"); // if true execute
   
   executeHandlePress = false; // toggle flag to false
   
   setTimeout(()=>{
   executeHandlePress = true; // reset after 5 seconds
   }, 5000);
    }
    
    
    
    
    
    
    
    
<input placeholder="type your message" id="messageInputField"  onKeyPress=" handleMessagePress(event)" />

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