Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Javascript: setInterval with click event not working

I'm making a small text adventure browser game and using javascript for the combat system. the query i've selected is a button for doing an attack.

The problem: When i hit attack button fast it puts out one random number, but when the interval loops multiple times and i hit the button slower i get multiple lines of output.I only need one output per click, I'm not sure how to fix this...

document.addEventListener("DOMContentLoaded", function(event) {
  console.log("DOM fully loaded and parsed");
  let dmg = 0;
  setInterval(tick, 500);

  function tick() {
    let charge = document.querySelectorAll("#host #boar1 a")[0];
    charge.addEventListener("click", chargefunction);

    function chargefunction() {
      dmg = Math.floor((Math.random() * 60) + 1);
      console.log(dmg);
    }
  }
});

Comments