- debounce.js
function debounce(fn, time) {
let timer;
return function() {
clearTimeout(timer);
timer = setTimeout(() => {
fn();
}, time)
}
}
- throttle.js
function throttle(fn, timer) {
let time;
return function() {
if (time) return;
time = setTimeout(() => {
fn()
time = null;
}, timer)
}
}
网友评论