0

I found code which runs a function when I scroll. And it works fine. But the problem is that it runs the function too many times per millisecond; can someone help me to add a function which allows me to choose the number of milliseconds between two function calls? Thanks :)

var runOnScroll =  function(evt) {
  console.log("hello");
};
var elements = document.querySelectorAll(".div2");
elements = Array.prototype.slice.call(elements);
elements.forEach(function(element) {
  window.addEventListener("scroll", runOnScroll, {passive: true});
});
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Martin
  • 79
  • 1
  • 6
  • 1
    You add a `scroll` listener multiple times. Notice that you run `addEventListener` inside a `.forEach` callback. – Eyal Dec 17 '20 at 13:38
  • Also, you can check this post to add a time limit to a listener https://stackoverflow.com/questions/14667010/limit-how-many-times-an-event-listener-can-trigger-every-second – Looper Dec 17 '20 at 13:53
  • Did you mean `element.addEventListener`? – Bergi Dec 17 '20 at 14:29
  • Thank you all ! I was really talking about window.addEventListener^^ – Martin Dec 20 '20 at 20:01

0 Answers0