Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

143
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda