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

前端 未结 6 2038
灰色年华
灰色年华 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);
      });
    });
    
    0 讨论(0)
  • 2020-12-13 05:24

    You may be interested in my bindDelayed jQuery mini-plugin. It:

    • Allows you to specify a delay before kicking off the request
    • Automatically cancels any previous requests that were scheduled to go off
    • Automatically cancels any in-air XHR requests that were in progress when you make your request
    • Only invokes your callback for the latest request
      • If the user types "s", waits long enough for the request to go out, and then types "a", and the response for "s" comes back before the response for "sa" you won't have to deal with it.

    The answer to the original question using bindDelayed would look like so:

    // Wait 200ms before sending a request,
    // avoiding, cancelling, or ignoring previous requests
    $('#searchMe').bindDelayed('keyup',200,'/RPCsearch.php',function(){
      // Construct the data to send with the search each time
      return {queryString:this.value};
    },function(html){
      // Use the response, secure in the knowledge that this is the right response
      $("#suggestions").html(html).show();
    },'html','post');
    

    In case my site is down, here's the plugin code for Stack Overflow posterity:

    (function($){
      // Instructions: http://phrogz.net/jquery-bind-delayed-get
      // Copyright:    Gavin Kistner, !@phrogz.net
      // License:      http://phrogz.net/js/_ReuseLicense.txt
      $.fn.bindDelayed = function(event,delay,url,dataCallback,callback,dataType,action){
        var xhr, timer, ct=0;
        return this.on(event,function(){
          clearTimeout(timer);
          if (xhr) xhr.abort();
          timer = setTimeout(function(){
            var id = ++ct;
            xhr = $.ajax({
              type:action||'get',
              url:url,
              data:dataCallback && dataCallback(),
              dataType:dataType||'json',
              success:function(data){
                xhr = null;
                if (id==ct) callback.call(this,data);
              }
            });
          },delay);
        });
      };
    })(jQuery);
    
    0 讨论(0)
  • 2020-12-13 05:29

    1 solution in psuedocode:

    OnKeyPress()
        txt = getTxt
        sleep(200)
        newTxt = getTxt
        if (txt == newTxt)  // nothing has been typed so do something
           run my thing
    
    0 讨论(0)
  • 2020-12-13 05:35

    this one is happy

     $(document).ready(function(){
    
       $("#searchMe").keyup(function () {
    
      try{window.clearTimeout(timeoutID);}catch(e){}
      timeoutID = window.setTimeout(run, 2000); //delay
    
      function run()
      {      //dowhatev
        var text = $("#searchMe").val();
        //$("#showit").html(text);       
      }     
     });   
    });
    
    0 讨论(0)
  • 2020-12-13 05:40

    I have found the best success when attaching the event to keypress, keydown, and keyup inputs. Safari/FireFox/IE all seem to handle special keypresses (delete, backspace, etc.) a bit differently but using all events together seems to cover it. The only way that running all events works though is to use setTimeout so that when they all fire it just resets the timer and ultimately the callback only gets executed once.

    var delay = 200;
    var search_timer = null;
    $("#searchMe").keydown(function(e) {
        if(search_timer) {
            clearTimeout(search_timer);
        }
        search_timer = setTimeout(lookup, delay);
    });
    $("#searchMe").keypress(function(e) {
        if(search_timer) {
            clearTimeout(search_timer);
        }
        search_timer = setTimeout(lookup, delay);
    });
    $("#searchMe").keyup(function(e) {
        if(search_timer) {
            clearTimeout(search_timer);
        }
        search_timer = setTimeout(lookup, delay);
    });
    
    0 讨论(0)
  • 2020-12-13 05:46

    You really ought to look at using the jQuery autocomplete plugin. I find this plugin to be very useful and it already does what you need. Look particularly at the delay option, which you can customize to change how long the plugin waits after a keystroke to run.

    0 讨论(0)
提交回复
热议问题