Search code examples
javascriptjquerystoppropagation

stop propagation if I type repeatingly


I'm building a custom search engine for a website and the search is done via AJAX on keyup event, so when you start typing ( if you type more than two characters and after the string is trimmed ) an AJAX call to the server is done.

It works perfectly, but there's one scenario I was thinking about, when you start typing four or as many as you want characters in a row, there will be as many calls as characters you type. Well that's going to make a lot of requests if you type 10 characters for instance.

In this case I was thinking if there is a simple way of stopping everything what was started before when such behavior occurs and pick up the search after you're done typing insanely (:


Solution

  • You can use Underscore debounce for that. Make the key up event call undersocre's function instead of your function, that way underscore will know when to call your function.