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

131
Visualizações
Debounce function triggers immediately on keyup, instead it must trigger after one second

Am trying to implement custom debounce function which must alert whatever data user has typed in a textbox after one second delay (After user has stopped typing). What am I doing wrong? It triggers the alert() immediately with single character.

function debounce(func, timeout) {
  let timer;
  return (...args) => {
    clearTimeout(timer);
    timer = setTimeout(() => {
      func.apply(this, args);
    }, timeout);
  };
}

function saveInput(args) {
  alert(args); //This triggers immediately
}
const processChange = (event) => {
  debounce(saveInput(event.target.value), 1000);
}
<input type="text" onkeyup="processChange(event)" />

Can you guys suggest if I miss anything here?

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

0

This has worked for me. However myInput is a global variable here.

function debounce(callback, delay) {
        let timeout;
        return function () {
            clearTimeout(timeout);
            timeout = setTimeout(callback, delay);
        }
    }

    const myInput = document.getElementById("myInput");

    function helloInput() {
        alert(myInput.value); 
    }

    myInput.addEventListener(
        "keyup",
        debounce(helloInput, 1000)
    );

    myInput.focus();
<input type="text" id="myInput" />

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