function throttle(func, wait) {
let timeout,self,args;
let previous = 0;
let later = function(){
previous = +new Date();
timeout = null;
func.apply(self,args);
}
let throttled = function(...param) {
self = this;
args = param;
let now = +new Date();
let remaining = wait-(now-previous);
if (remaining<=0 || remaining>wait) {
if(timeout){
clearTimeout(timeout)
timeout=null;
}
previous = now;
func.apply(self, args);
}else if(!timeout){
timeout=setTimeout(later,remaining);
}
};
return throttled;
}
window.addEventListener('scroll',throttle(fn,500));