New to js-classes so this might be a rookie mistake, but here goes:
I'm trying to run a throttled event when scrolling
scrollEvent = () =>{
console.log('test')
}
When I have a throttle like this:
throttle = (fn, wait) => {
var time = Date.now();
return function() {
if ((time + wait - Date.now()) < 0 && !transitioning) {
fn();
time = Date.now();
}
}
}
window.addEventListener('scroll', throttle(scrollEvent, 50));
It works fine, but when I try to move the throttle to a class:
Throttle = class{
constructor(){
this.on = true;
}
run(fn, wait) {
var time = Date.now();
return function() {
if ((time + wait - Date.now()) < 0 && this.on) {
fn();
time = Date.now();
}
}
},
scroll = new Throttle();
window.addEventListener('scroll', scroll.run(scrollEvent, 50));
The event won't trigger on scroll.