javascript - How do I make my live jQuery search wait a second before performing the search? -
I have a search input that sends the data to a php file from an input as I write the PHP file to my database Finds a list and shows a list of search options. You know, AJAX is looking for style live.
My problem is, if you type something very fast, then it can only stop searching for 1 or 2 characters, even if 10 others have been typed it creates some problems .
My jquery looks like this:
$ (document) .ready (function () {$ ('# searchMe') .keyp (function () {lookup (This.value);});});
and
function lookup (search input) {if (searchinput.length == 0) {// hide suggestion box $ ("#suggestions") Hide () .; } And {$ ('# Loading'). FadeIn (); $ .post ("/ RPCsearch.php", {queryString: "" + searchinput + ""}, function (data) {if (data.lamp & gt; 0) {$ ("# suggestion") .html ) Show (); $ ('# Loading'). FadeOut ();}}); }} // Lookup
So I'm just curious, how can I do this so that my script will wait until I finish typing before running the function? My argument says something like that one key has not been pressed for 200 microsoft seconds, run the function, otherwise press for a while.
How is this done?
Easy, by using Of course you only want to run one timer once, so it is important to use it at the beginning of the function ...
$ (function () {var timer; $ ("# searchMe" ) .keyup (function () {clearTimeout}} var ms = 200; // milliseconds var val = this.value; timer = setTimeout (function () {lookup (val);}, ms);});} );
Comments
Post a Comment