@modern-kit/utils
Version:
28 lines (24 loc) • 721 B
JavaScript
;
var commonDebounce = require('../debounce/index.cjs');
function throttle(func, wait, options = {}) {
const { signal, leading = true, trailing = true } = options;
const debounced = commonDebounce.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;
}
exports.throttle = throttle;
//# sourceMappingURL=index.cjs.map