Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trying to perform a slightly different search (Timer, event listener, event dispatch?)

How I need to:

  1. First keyup on (search input) fires up search countdown timer and set timer to 1s.
  2. Every next search keyup on (search input) reset search countdown timer to 1s.
  3. When (search input) no longer updated and stay the same –
  4. Search countdown timer reached 0s and performing a search.

Why I need to (this way):

I have large database of records and using ajax method to fetch through. Every time keyup on (search input) send a request… (it’s resource waste and uncomfortable to watch how page refresh every keyup). So my way is to create "typing phase" – what I mean describe above.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Only pure JS, no libraries! Thanks.

>Solution :

var timer=null;
document.getElementById("myInput").onkeyup = ()=>{
  clearInterval(timer);
  timer = setInterval(()=>{
            console.log("call ajax search function here."); 
            clearInterval(timer);
          },1000);
};
<input type="text" id="myInput">
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading