Throttle是web应用中经常用到的技巧,通常情况下你应该使用现有的实现,比如lodash.throttle() 你能够自己实现一个基本的throttle()吗? 再次说明一下,throttle(...展开
/**
* @param {Function} func
* @param {number} wait
*/
function throttle(func, wait) {
let waiting = false;
let lastArgs;
return function () {
if (!waiting) {
waiting = true;
func.apply(this, arguments);
setTimeout(() => {
waiting = false;
if (lastArgs) func.apply(this, lastArgs);
}, wait);
} else {
lastArgs = [...arguments];
}
}
}