Throttle event calls in jQuery

前端 未结 7 867
自闭症患者
自闭症患者 2020-11-27 03:52

I have a keyup event bound to a function that takes about a quarter of a second to complete.

$(\"#search\").keyup(function() {
  //code that tak         


        
7条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 04:19

    Here is a potential solution that doesn't need a plugin. Use a boolean to decide whether to do the keyup callback, or skip over it.

    var doingKeyup = false;
    
    $('input').keyup(function(){
        if(!doingKeyup){
            doingKeyup=true;
            // slow process happens here
            doingKeyup=false;
        }
    });
    

提交回复
热议问题