Skip to content
Advertisement

Why e.repeat is not working and have no errors in the console?

JavaScript

this code stores when any of the 'q', 'w', 'a', 's' key is clicked in an array. this works fine until I click and hold any key which results in repeated input. for dealing with this I used if (e.repeat) { return } but this is not working and does not give any error in the console. Help Me to find what I am doing wrong.

here is the remaining relevant code if it helps

JavaScript

Advertisement

Answer

It looks like the repeat property isn’t defined at all in the jQuery event (e) object.

JavaScript
JavaScript
JavaScript

But using addEventListener, it works with keydown. You will notice that the very first log is false and all the other ones are true.

JavaScript
JavaScript
JavaScript

Here is a code suggestion for you:

JavaScript
JavaScript
JavaScript

And about the keyup handler: you may want to remove the active class only on the relevant element instead of all the .box elements…


Additionally: It is a good practice NOT to have event handlers defined in another one to avoid registering multiple times the same handler.

Advertisement