js防抖 、节流函数
2021-09-15 本文已影响0人
无迹落花
节流
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);
}
}