fix time problem

setTimeout would go wrong in some browser when the second argument is less than zero
This commit is contained in:
Henry
2018-10-08 21:48:19 +08:00
committed by GitHub
parent a2e49e3ba7
commit 78c6f6d395

View File

@ -19,12 +19,14 @@ const throttle = (fn, wait) => {
inThrottle = true; inThrottle = true;
} else { } else {
clearTimeout(lastFn); clearTimeout(lastFn);
let time = 0;
lastFn = setTimeout(function() { lastFn = setTimeout(function() {
if (Date.now() - lastTime >= wait) { if (Date.now() - lastTime >= wait) {
fn.apply(context, args); fn.apply(context, args);
lastTime = Date.now(); lastTime = Date.now();
} }
}, wait - (Date.now() - lastTime)); time = wait - (Date.now() - lastTime)
}, time>0 ? time : 0);
} }
}; };
}; };