Skip to content

Instantly share code, notes, and snippets.

@vhogemann
Created May 26, 2017 11:14
Show Gist options
  • Save vhogemann/1ea27d97575863f88c8c9bd9cf3009a7 to your computer and use it in GitHub Desktop.
Save vhogemann/1ea27d97575863f88c8c9bd9cf3009a7 to your computer and use it in GitHub Desktop.
Simple method Debouncer for typescript class methods
export function Debounce(wait: number) {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
let timeout;
const original:Function = descriptor.value;
const debounced = function() {
const context = this;
const args = arguments;
const later = function() {
timeout = null;
original.apply(context, args);
}
clearTimeout(timeout);
timeout = setTimeout(later, wait);
}
descriptor.value = debounced;
return descriptor;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment