utils/ts-src/throttle.ts
Martin Donnelly c30b36b3f6 init
2020-11-17 11:16:34 +00:00

23 lines
450 B
TypeScript

/**
* 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);
}
};
}