delaying actions between keypress in jQuery

后端 未结 5 1236
刺人心
刺人心 2020-12-08 02:23

How can I delay actions between keypress in jQuery. For example;

I have something like this

 if($(this).val().length > 1){
   $.post(\"stuff.php\"         


        
5条回答
  •  再見小時候
    2020-12-08 03:17

    Nick's answer is perfect but if handling first event immediately is critical then I think we can do:

    $(selector).keyup(function(e){ //or another event
    
        if($(this).val().length > 1){
            if !($.data(this, 'bouncing-locked')) {
    
                $.data(this, 'bouncing-locked', true)
    
                $.post("stuff.php", {nStr: "" + $(this).val() + ""}, function(data){
                    if(data.length > 0) {
                        $('#suggestions').show();
                        $('#autoSuggestionsList').html(data);
                    }else{
                        $('#suggestions').hide();
                    }
               });
    
                self = this
                setTimeout(function() {
                    $.data(self, 'bouncing-locked', false);
    
                    //in case the last event matters, be sure not to miss it
                    $(this).trigger("keyup"); // call again the source event
                }, 500)
            }
        }
    });
    

提交回复
热议问题