开发者

jQuery: keydown event fires multiply times

At present, when I press the Ctrl button and hold it press开发者_Go百科ed for a while, the handler that I binded to the keydown event with the help of jQuery is triggered multiply times - I would like to avoid triggering it more than once per a separate press. How would I accomplish that?


If, whatever you are doing, does not have to be triggered immediately, you could listen to keyup [docs] instead.

Otherwise, you could set a flag on keydown and clear it on keyup:

$element.keydown(function() {
    var data = $(this).data();

    if(!data['pressed']) {
        data['pressed'] = true;
        // do stuff
    }
}).keyup(function() {
    $(this).data('pressed', false);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜