FE-Interview
FE-Interview copied to clipboard
节流
扫描下方二维码,获取答案以及详细解析,同时可解锁800+道前端面试题。
function throttle(fn, delay) {
let called = false
return function() {
if (!called) {
called = true
setTimeout(() => {
fn.apply(this, arguments)
called = false
}, delay)
}
}
}
function throttle(func, wait) {
let context, arges;
let prevous = 0;
return function () {
let now = new Date();
context = this;
arges = arguments;
if (now - prevous > wait) {
func.apply(context, arges);
prevous = now;
}
};
}
function throttle(executer, wait, ...defaultParams) {
let token = true;
return function throttleWrappedExecuter(...args) {
if (!token) {
return;
}
token = false;
setTimeout(() => {
executer.call(this, ...defaultParams, ...args);
token = true;
}, wait);
};
}
const printHello = throttle(
(...args) => {
console.log("hello", ...args);
},
5000,
"DEV"
);