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

24 lines
518 B
TypeScript

/**
* Debounce the calling of a function
* @param fn The function to be debounced
* @param time How long to wait
* @returns {Function}
*
* @signature
* U.debounce(fn, time)
*
*/
export function debounce(fn: Function, time: number): Function {
let timeout: NodeJS.Timeout;
return function (...args: any): any {
// <-- not an arrow function
// @ts-ignore
const functionCall = () => fn.apply(<any>this, args);
clearTimeout(timeout);
timeout = setTimeout(functionCall, time);
};
}