1
0
mirror of https://github.com/ZeroCatDev/Classworks.git synced 2025-07-05 02:59:23 +00:00
Classworks/src/utils/debounce.js
SunWuyuan 10b7f3784f
1
2025-03-08 21:51:00 +08:00

28 lines
578 B
JavaScript

export function debounce(fn, delay) {
let timer = null;
return function (...args) {
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, args);
}, delay);
};
}
export function throttle(fn, delay) {
let timer = null;
let last = 0;
return function (...args) {
const now = Date.now();
if (now - last < delay) {
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
last = now;
fn.apply(this, args);
}, delay);
} else {
last = now;
fn.apply(this, args);
}
};
}