jQuery 热键触发太快:如何捕获较慢的按键?
我使用 jQuery 热键插件,代码如下:
$(document).bind('keydown', 'right', function(){
console.log('fired');
display_info();
});
按下右键时,display_info();大约被调用3-4次。如何减慢按键速度?
谢谢!!
I'm using jQuery hotkeys plugin with the following code:
$(document).bind('keydown', 'right', function(){
console.log('fired');
display_info();
});
When the right button is pressed, the display_info(); is called about 3-4 times. How do I catch the keypresses slower?
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用 keyup 而不是 keydown,或者构建一个计时器。
这是我制作的一个快速而肮脏的计时器,它应该可以让您很好地了解需要做什么: http ://jsfiddle.net/Grimdotdotdot/bnM4T/6/
Try keyup rather than keydown, or build a timer.
Here's a quick and dirty timer I knocked up which should give you a good idea of what you need to do: http://jsfiddle.net/Grimdotdotdot/bnM4T/6/
我想添加此内容以防其他人遇到此问题。
你遇到的问题听起来和我遇到的问题一模一样。
如果我绑定多个快捷方式事件,则每个事件的执行时间将比文档中快捷方式绑定的总数少一次。我有这样的事情:
对于每个快捷方式绑定,我会收到两次警报。我解决这个问题的方法是取消绑定快捷方式并添加一个方法来处理警报和取消绑定。
以这种方式使用 unbind 允许我只执行每个快捷方式的代码一次,同时保留每次按下快捷方式时的快捷方式功能,并使我的代码相对干燥。
I wanted to add this in case anyone else was experiencing this issue.
The problem you were having sounds exactly like the one I was having.
If I bound multiple shortcut events then each one would execute one less time than the total number of shortcut bindings in the document. I had something like this:
For each shortcut bind, I would get the alert two times. The way I solved this was to unbind the shortcut and add a method to handle the alert and unbind.
Using unbind in this way allowed me to execute each shortcut's code only once, while retaining the shortcut functionality each time the shortcut is pressed, and keep my code relatively DRY.