Optimised search using Ajax and keypress

前端 未结 4 1193
南方客
南方客 2021-01-30 02:35

I have the following code that I want to use to search a database as a user is typing into a textbox. The code below works fine but it seems a little inefficient, as if a user i

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-30 03:04

    What I would do is each key press use a setTimeout function with the desired delay. So that function will fire after that timeout. Each key press then delete the timer and set a new one, with clearTimeout();

    See here for some examples, scrolling past all the adverts.

    http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/

提交回复
热议问题