JavascriptJQuery 如何防止按住键时函数重复执行?

问题描述 投票:1回答:2

|(e.keyCode)); switch (arrows){ case 37: ...

$(document).bind('keyup', function(e) {    
    arrows=((e.which)||(e.keyCode));    
    switch (arrows){    
       case 37:
          executeMyfunction();     
          break;    
    }    
});

I believe you have to unbind the event. I think it should not be executeMyfunction(), but

or

.keyupProbably something like this.keyupYou will have to rebind the function on keydown if you want to use the event again:

Look at:
javascript jquery keypress jquery-events onkeyup
2个回答
1
投票

被反复调用。keyup如何防止这种情况发生?keypress我想 keydown 只有当键被释放时才会触发,但它不工作(这就是为什么我使用了

;

$(document).bind('keydown', function(e) {    
    arrows=((e.which)||(e.keyCode));    
    switch (arrows){    
       case 37:    
          executeMyfunction();    
          $(document).unbind('keydown');    
          break;    
    }
});

也不工作)。)keyup

$(document).bind('keyup', function(e) {        
    switch (arrows){    
      case 37:
         $(document).bind('keydown');
         break;
    }    
})

假设我们有以下JQuery代码。$(document).bind('keyup', function(e) { arrows=((e.which)/api.jquery.comunbind)


0
投票

好吧,你可以随时 退弹 函数的调用。也就是说,如果你想让你的函数在事件短时间内没有被触发后被调用。

© www.soinside.com 2019 - 2024. All rights reserved.