user2660940
user2660940

Reputation: 51

jQuery keydown : Wait for previous event to finish

We are using the following jQuery code for our site:

$(document).keydown(function (x) {
    if (x.keycode == 9) {
        // perform some action
    }
})

However, the way it currently works is that when you press this key in quick succession, the action does not get performed successively, but rather it ignores any key presses until the current action is completed. We wish to have it so that any additional key presses are queued, and that any key presses made during the action cause the action to be performed as well but only after the last iteration of the action has been performed.

So for example, if you press the key 9 times, the action is performed 9 times, regardless if the action is being performed during the keypress. The way I have it now is that you could press the key 9 times but the action will only be performed n times for n <= 9, because the keypress is not being recognized while the action is performed.

Any ideas?

EDIT: According to Hunter, I need to use some sort of queue to queue the key presses...how would I go about that?

Upvotes: 1

Views: 650

Answers (1)

hunter
hunter

Reputation: 63512

var inProgress = false;

$(document).keydown(function (x) {
    if (x.which == 9) {
        if (!inProgress)
        {
            inProgress = true;
            // perform some action
            inProgress = false;
        }
    }
})

Upvotes: 2

Related Questions