click function in js stacking up

I need some help with my js code…

I am currently trying to make some keyboard shortcuts to make my job a little bit faster and I wrote this code that runs using violent monkey everytime I load the site.

var reloadButton = document.getElementById("btnSearchAll");
document.addEventListener('keydown', (event) => {
    if(event.altKey && event.key == "r") {
    reloadButton.click();
  }
});

The problem is that for every time I use this shortcut, the click function of the button I am trying to click stacks up. the first time I use it, it clicks once, the second time it fires twice and so on.

I tried making some logic into it using if statements and adding an event listener for when I let go of the keys but it doesn’t seem to work.

I’m not really that good with javascript so I’m asking for your help. what does the issue seem to be?

Leave a Comment