debounce-throttling
Version:
debounce-throttle is a lightweight npm package designed to simplify and enhance event handling in JavaScript applications. With this package, developers can effortlessly implement debouncing and throttling functionalities to optimize performance and impro
38 lines (37 loc) • 873 B
JavaScript
// src/debounce-throttle.ts
var throttle = (cb, time) => {
if (time === void 0)
throw new TypeError("time is undefined");
if (time < 0)
throw new RangeError("time cannot be negative");
if (typeof cb !== "function")
throw new TypeError("callBack is not a function");
let last = 0;
return (...args) => {
let now = Date.now();
if (now - last >= time) {
cb(...args);
last = now;
}
};
};
var debounce = (cb, time) => {
if (time === void 0)
throw new TypeError("time is undefined");
if (time < 0)
throw new RangeError("time cannot be negative");
if (typeof cb !== "function")
throw new TypeError("callBack is not a function");
let timer;
return (...args) => {
if (timer)
clearTimeout(timer);
timer = setTimeout(() => {
cb(...args);
}, time);
};
};
export {
debounce,
throttle
};