0

So, I have this code, in order to make a button autoclick.

Here is some code:

var int;
x = document.querySelectorAll("button");

for (var i = 0; i < x.length; i++) {
  function yeet() {
    x[i].click();
  }
  
  x[i].onmouseenter = function() {
    int = setInterval(yeet, 0);
  };
  
  x[i].onmouseleave = function() {
    clearInterval(int);
  };
}
<button>one</button>
<button>two</button>

The interval works, and hovering does repetitively does an action fast. However, the console says "Can't read properties of undefined (reading click)". Therefore, it doesn't auto-click any button. Does anyone know a solution? As always, thanks so much for responding!!! :D

adiga
  • 34,372
  • 9
  • 61
  • 83
  • Why is `yeet` inside of your for loop – Thomton Nov 11 '22 at 15:48
  • A better solution would be `const timers = new WeakMap(); addEventListener("mouseenter", ({ target }) => { const button = target.closest("button"); if(button){ timers.set(button, setInterval(button.click.bind(button), 0)); } }, { capture: true }); addEventListener("mouseleave", ({ target }) => { const button = target.closest("button"); if(button){ clearInterval(timers.get(button)); } }, { capture: true });`. – Sebastian Simon Nov 11 '22 at 15:49

0 Answers0