How do I make my live jQuery search wait a second before performing the search?

前端 未结 6 2057
灰色年华
灰色年华 2020-12-13 04:52

I\'ve got a search input which sends data from an input to a php file as I type. The php file does a search on my database and shows up a list of search options. You know, t

6条回答
  •  天命终不由人
    2020-12-13 05:22

    Easy, using setTimeout. Of course you only want one timer going at once, so it's important to use clearTimeout at the beginning of the function...

    $(function() {
      var timer;
      $("#searchMe").keyup(function() {
        clearTimeout(timer);
        var ms = 200; // milliseconds
        var val = this.value;
        timer = setTimeout(function() {
          lookup(val);
        }, ms);
      });
    });
    

提交回复
热议问题