I have the following code:
undoButton.onclick = undoFunction; document.addEventListener("keydown", (e) => { if ((e.ctrlKey || e.metaKey) && e.code === "KeyZ") { e.preventDefault(); undoFunction(); } }); function undoFunction() { console.log("undo function..."); } When I click the button, as excepted, the function code runs once, and so does the console.log, but when I use the key stroke, the function is running a multiple times, up to hundreds of so-called loops at some scenarios. Any suggestion why? I tried to used e.repeat = false but had no luck. Thanks!
.addEventListener()does just that: it adds an event listener, without removing any previously-added event listeners. If you do that over and over again, all of the added event listeners will run on every "keydown" event.