Created
May 26, 2017 11:14
-
-
Save vhogemann/1ea27d97575863f88c8c9bd9cf3009a7 to your computer and use it in GitHub Desktop.
Simple method Debouncer for typescript class methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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