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

171
Visualizações
Why does console.log("Called"); is called only once inside the debounce function?

In below code why console.log("Called"); is only coming once though the debounce function is called again and again on every keyup event

 const txt = document.getElementById("text");
        let label = document.getElementById("label");
    
        function debounce(fun,delay) {
            let time;
            console.log("Called");
            return function(args) {
                if(time) {
                    clearTimeout(time);
                }
                time = setTimeout(() => {
                    fun(args);
                },delay);
            }
        }
    
        function callback(e) {
            label.innerHTML = e.target.value;
        }
        
        txt.addEventListener("keyup", debounce(callback,700));
    </script>
<html>
    <body>
        <input type="text" id="text" placeholder="Enter something..."><br />
        <label id="label"></label>
    </body>
</html>

?

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

0

It is being called while setting up the event listener. Even before any keyup event. If you want to log when the function is being called. Log it inside the function that is being returned from debounce.

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