简体   繁体   English

如何在 jQuery 键盘上实现延迟?

[英]How do I implement a delay on jQuery keyup?

I'm making a list filter and want a delay in case the user is a fast typer.我正在制作一个列表过滤器,并希望在用户是快速打字机的情况下延迟。 Looking at different solutions for similar questions hasn't helped me and I don't understand the logic they implement.查看类似问题的不同解决方案对我没有帮助,我也不理解他们实现的逻辑。

This is my current code:这是我当前的代码:

$.fn.filterList = function(){   
    var inputFilter = $(this);
    var list        = $('#' + inputFilter.data('list'));
    var listItems    = list.children('li');

    inputFilter.keyup(function(){

        setTimeout(function () {
            var term = inputFilter.val().toLowerCase();

            listItems.each(function(i, e){
                var city = ($(e).text()).toLowerCase();

                if(city.startsWith(term)){
                    console.log(city);
                }
            });
        }, 800);
    });

};

$('.my-input').filterList();

The problem with this is that it will trigger on each keyup, no matter how fast the user types.这样做的问题是它会在每次按键时触发,无论用户键入的速度有多快。

How can I implement a delay so that it does not trigger for each keyup ?如何实现延迟,以便它不会为每个keyup触发?

On each successive keypress you need to clear the previous timer so that the function only fires X milliseconds after typing ends.在每次连续keypress您都需要清除前一个计时器,以便该功能在键入结束后仅触发 X 毫秒。 Try this:尝试这个:

var timer;

inputFilter.keyup(function() {
  clearTimeout(timer);
  timer = setTimeout(function() {
    var term = inputFilter.val().toLowerCase();

    listItems.each(function(i, e) {
      var city = $(e).text().toLowerCase();
      if (city.startsWith(term)) {
        console.log(city);
      }
    });
  }, 800);
});

Here's a simplified working example:这是一个简化的工作示例:

 var timer; $('#foo').keypress(function() { clearTimeout(timer); timer = setTimeout(function() { $('div').fadeIn('fast').delay(1000).fadeOut('fast'); }, 800); });
 div { display: none; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input id="foo" /> <div>You stopped typing 1 second ago</div>

var delay = (function(){
  var timer = 0;
  return function(callback, ms){
    clearTimeout (timer);
    timer = setTimeout(callback, ms);
  };
})();

inputFilter.keyup(function() {
    delay(function(){
      var term = inputFilter.val().toLowerCase();

            listItems.each(function(i, e){
                var city = ($(e).text()).toLowerCase();

                if(city.startsWith(term)){
                    console.log(city);
                }
            });
    }, 800 );
});

try the following code:试试下面的代码:

Html:网址:

<input type="text" id="inputtext" />

Jquery code:查询代码:

<script type="text/javascript">
        $(document).ready(function () {
            $('#inputtext').keyup(function () {
                setTimeout(function () {
                    alert("Hi");
                }, 5000);
            });
        });
    </script>

On each keyup count clicks and after some of them trigger function.在每个 keyup 计数点击和其中一些之后触发功能。
Pro-tip: And use专业提示:并使用

.on('keyup', function(){}).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM