utils/ts-src/throttle.ts
2022-10-21 12:08:36 +01:00

23 lines
450 B
TypeScript
Executable File

/**
* Throttle the calling of a function
* @returns {Function}
* @param callback
* @param limit
*
* @signature
* U.throttle(callback, limit)
*/
export function throttle(callback: Function, limit: number): Function {
var wait: boolean = false;
return function (...args: any): any {
if (!wait) {
callback.apply(null, args);
wait = true;
setTimeout(function () {
wait = false;
}, limit);
}
};
}