TGear
TGear

Reputation: 21

jQuery: keydown event fires multiply times

At present, when I press the Ctrl button and hold it pressed 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?

Upvotes: 2

Views: 1512

Answers (1)

Felix Kling
Felix Kling

Reputation: 817238

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);
});

Upvotes: 3

Related Questions