typedash
Version:
modern, type-safe collection of utility functions
40 lines (38 loc) • 907 B
JavaScript
;
// src/functions/debounce/debounce.ts
function debounce(func, delay, options = {}) {
let timeoutId = null;
let isCalled = false;
let latestArguments;
function debouncedFunction(...args) {
latestArguments = args;
if (options.leading && !isCalled) {
func(...args);
isCalled = true;
}
if (timeoutId !== null) {
clearTimeout(timeoutId);
}
timeoutId = setTimeout(() => {
if (!options.leading) {
func(...args);
}
isCalled = false;
}, delay);
}
debouncedFunction.flush = () => {
if (timeoutId !== null) {
clearTimeout(timeoutId);
}
func(...latestArguments);
};
debouncedFunction.clear = () => {
if (timeoutId !== null) {
clearTimeout(timeoutId);
}
};
return debouncedFunction;
}
exports.debounce = debounce;
//# sourceMappingURL=out.js.map
//# sourceMappingURL=index.cjs.map