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

199
Views
How can I prevent firing setTimeout function if user performs actions too fast in js?

I have this part of code:

$('#clientsfilter').on('input', function() {
     var string = $('#clientsfilter').val();
     if (string != "") {
         //console.log("input is not empty");
         var timeoutid = setTimeout(function() {
             // some stuff
             console.log("fired!");   
         }, 1500);
         console.log("timeoutid is "+timeoutid);
     }
     else
     {
        // console.log("input is empty");
     }    
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="clientsfilter" type="text" size="27">

When a user types something, it perform some actions after 1,5 seconds. But if a user will type one symbol after another too fass (time between symbols less than 1,5 seconds), the setTimeout() stuff will fire every time user adds symbol (or deletes). I want to prevent firing setTimeout function too many times, only if 1.5 seconds passed from last typing. I found information about

clearTimeout(timeoutid);

But how can I use it my situation?

about 4 years ago · Juan Pablo Isaza
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!