Max
Max

Reputation: 1131

Suspend JavaScript keydown event

Is it possible to suspend keydown (and following keypress, keyup) event in JavaScript?

The case is:

Press some button (for example, "Enter" key), sleep for 5 seconds and continue this event

Upvotes: 0

Views: 248

Answers (1)

Brian Ustas
Brian Ustas

Reputation: 65499

The below pure JS solution will prevent multiple events from firing if the timer is running.

Here's a demo.

document.onkeydown = (function() {
    var isActive = false;

    return function(e) {
        if (e.keyCode === 13 && !isActive) {
            isActive = true;
            setTimeout(function() {
                isActive = false;

                // Your code here...

            }, 5000);
        }
    };

})();

Upvotes: 1

Related Questions