Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!