I have an eventlistener that looks like this:
window.addEventListener('scroll', scroll.throttle(
triggered,
{state: state, wrapper: wrapper, children: children, scroll: scroll},
50
));
And I have a class that looks like this:
Scroll = class{
constructor(){
this.on = true;
}
throttle(fn, v, wait){
var time = Date.now();
return () => {
if ((time + wait - Date.now()) < 0 && this.on) {
fn(v);
time = Date.now();
}
}
}
triggered(o){
if(o.state.check !== 0){
o.scroll.on = false;
o.wrapper.classList.toggle('flic-down', o.state.check === 1)
o.wrapper.classList.toggle('flic-up', o.state.check === -1)
o.state.update();
o.wrapper.classList.add('flic-transition')
setTimeout(()=>{this.changeDone(o)}, 1200);
}
}
changeDone(o) {
o.wrapper.classList.remove('flic-transition', 'flic-up', 'flic-down');
o.children.setClasses(o.state.state);
o.wrapper.getElementsByClassName('flic-active')[0].scrollIntoView(true);
o.scroll.on = true;
}
},
I don't like passing state, wrapper, children and scroll as variables. I would prefer to store them in the class when instantiating them. I understand the problem is that "this" won't be passed correctly and that it can be bound. But because the throttle function I don't understand how to pass this.