@modern-kit/utils
Version:
26 lines (23 loc) • 678 B
JavaScript
import { debounce } from '../debounce/index.mjs';
function throttle(func, wait, options = {}) {
const { signal, leading = true, trailing = true } = options;
const debounced = debounce(func, wait, { signal, leading, trailing });
let pendingAt = null;
const throttled = function(...args) {
const now = Date.now();
if (pendingAt == null) {
pendingAt = now;
} else {
if (now - pendingAt >= wait) {
pendingAt = now;
debounced.cancel();
}
}
debounced(...args);
};
throttled.cancel = debounced.cancel;
throttled.flush = debounced.flush;
return throttled;
}
export { throttle };
//# sourceMappingURL=index.mjs.map