@explita/daily-toolset-form
Version:
A lightweight form toolkit for React built with developer ergonomics in mind. Includes a flexible Form component, useForm, useField, and useFormContext hooks for managing form state and validation with ease. Designed to simplify complex forms while remain
29 lines (28 loc) • 986 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.debounce = debounce;
/**
* Creates a debounced function that delays invoking the provided function until
* after `delay` milliseconds have passed since the last time the debounced
* function was called. The debounced function returns a function that can be
* used to cancel the pending call to the underlying function.
*
* @param fn The function to debounce.
* @param delay The number of milliseconds to delay calling the function.
* @returns A debounced function with an extra `cancel` method to cancel the pending call.
*/
function debounce(fn, delay) {
let timeout;
function debounced(...args) {
if (timeout)
clearTimeout(timeout);
timeout = setTimeout(() => fn.apply(this, args), delay);
}
debounced.cancel = () => {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
};
return debounced;
}