/**
* @desc 函数节流
* @param func 函数
* @param wait 延迟执行毫秒数
* @param type 1 表时间戳版,2 表定时器版
*/
function throttle(func, wait, type){
if (type === 1) {
var previous = 0;
} else if (type === 2) {
var timeout;
}
return function() {
var context = this;
var args = arguments;
if (type === 1) {
var now = Date.now();
if (now - previous > wait) {
func.apply(context, args);
previous = now;
}
} else if (type === 2) {
if (!timeout) {
timeout = setTimeout(() => {
timeout = null;
func.apply(context, args);
}, wait);
}
}
};
};
函数节流的写法
于 2021-10-12 17:37:32 首次发布