js-classes的新手,所以这可能是一个新手的错误,但这里有:
我正在尝试在滚动时运行受限制的事件
scrollEvent = () =>{
console.log('test')
}
当我有这样的油门时:
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));
它工作正常,但是当我尝试将油门移到一个类时:
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));
滚动时不会触发事件。