节流
function throttle(fn, delay) {
let lastTime = 0;
return function () {
let nowTime = Date.now();
if (nowTime - lastTime > delay) {
fn().call(this);
lastTime = nowTime;
}
}
}
防抖
function debounce(fn, delay) {
var timer = null;
return function () {
clearTimeout(timer);
timer = setTimeout(function () {
fn.call(this);
}, delay);
}
}
网友评论