export interface Debouncer { debounce ( callback ) : void ; cancel ( ) : void ; } export class TimeoutDebouncer implements Debouncer { private timer : number = null ; callback : Function ; constructor ( public wait ) { } debounce ( callback ) { this . callback = callback ; this . schedule ( ) ; } schedule ( ) { this . cancel ( ) ; if ( this . wait <= 0 ) { this . callback ( ) ; } else { this . timer = setTimeout ( this . callback , this . wait ) ; } } cancel ( ) { if ( this . timer ) { clearTimeout ( this . timer ) ; this . timer = null ; } } }