1、防抖
const debounce = function (f1, wait) {
let executeTime = null;
return function () {
clearTimeout(executeTime);
executeTime = setTimeout(f1, wait, ...arguments);
};
};
2、节流(时间戳)
const throttle = function (func, delay) {
let prev = Date.now();
return function () {
if (Date.now() - prev >= delay) {
func.apply(this, arguments);
prev = Date.now();
}
};
};
3、节流(定时器)
const throttle = function (f1, wait) {
let executeTime = null;
return function () {
if (!executeTime) {
const argus = arguments;
executeTime = setTimeout(function () {
f1(...argus);
executeTime = null;
}, wait);
}
};
};
网友评论