How to throttle callback of jQuery event?

后端 未结 4 1016
闹比i
闹比i 2020-12-31 22:47

Ok, so I do search like in google, you type text in input and it gives you entries instantly. But I don\'t like that. I use something like that $(\"TEXTINPUT\").keyup(

4条回答
  •  既然无缘
    2020-12-31 23:04

    Here's how I usually approach it:

    $(function(){
      var tiTO, jqXHR;
      $('textinput').keyup(function(){
        if (tiTO) clearTimeout(tiTO);
        if (jqXHR && jqXHR.abort) jqXHR.abort();
        tiTO = setTimeout(function(){
          jqXHR = $.ajax({....});
          //Ajax call to PHP
        },2000);
      });
    });
    

    Each keyup resets the timeout, and aborts any active ajax.

提交回复
热议问题