Elaine Elaine - 10 months ago 63
Ajax Question

jQuery autocomplete, Can I have the previous ajax calling stopped when I raise a new one?

Now I'm using jQuery auto-complete, but I found a performance-related issue - if I input 'abc', it will be scanning by 'a', by 'ab', and by 'abc', at a once, how can I stop the previous 'a', 'ab' when ajax calling is doing searching by 'abc'?

The similar case is -,
I use jQuery DataTables, and want to do some searching like, I typed something to search (call search web method), and clicked button - 'Search' 3 times at a once, or changed search text just when I clicked 'Search', and raise a new search, how can I stop the previous useless ajax calling?

Hope I made myself clear.
Thanks in advance.

Answer Source

Assuming you are using jQuery UI Autocomplete:

  1. You can specify the minimum length of string before starts searching. (default is 3 I guess)
  2. You can also specify the delay between last keystroke and ajax call. Usually 2-300ms should do.
  3. AjaxQueue might help you to clean some stuff. Dunno how (I never need it), but worth a shot :)