@embrace-io/web-sdk
Version:
23 lines (22 loc) • 864 B
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
//#region src/utils/throttle.ts
/** throttle wraps a function so it can only be called once, and then a time window must pass before it can be called again
* any subsequent calls before the time window has passed will be ignored.
*
* NOTE: if the wrapped function makes use of "this", it must be properly bind from the caller, as this wrapper will not bind it.
* For this, we suggest you always pass functions declared as fat arrow functions, as they have their "this" lexically defined.
* */
const throttle = (func, timeout = 1e3) => {
let isWaiting = false;
return (...args) => {
if (isWaiting) return;
func(...args);
setTimeout(() => {
isWaiting = false;
}, timeout);
isWaiting = true;
};
};
//#endregion
exports.throttle = throttle;
//# sourceMappingURL=throttle.cjs.map