What I wanted: Add an event listener to window on scroll, but only when a certain function is fired. The way I wanted it to work is:
foo()window.addEventListener("scroll", bar());bar() be executed upon every single scroll (until I decide to removeEventListener() somewhere else).What happens: bar() only gets executed once - during the life span of foo(), I suppose.
A workaround is to
bar() inside if (some_boolean === true) {,some_boolean variable (declared globally) to true before foo() ends.That way, I got the scroll functionality I needed, but now bar() and its boolean check gets executed upon every scroll, when there's absolutely no need to.
Question: How do I add the event handler so that it runs bar() efficiently?
bar() appears to be called, not referenced at window.addEventListener("scroll", bar()); . Try referencing bar as handler to call when scroll event occurs
window.addEventListener("scroll", bar);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With