mirror of
https://github.com/ZeroCatDev/Classworks.git
synced 2025-07-05 02:59:23 +00:00
28 lines
578 B
JavaScript
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);
|
|
}
|
|
};
|
|
}
|