import { Renderer2 , NgZone } from '@angular/core' ; import { Subject } from 'rxjs/Subject' ; export class EventDebouncer { constructor ( private _zone , private _renderer ) { } listen ( target , eventName , listener , debounceTime = 40 ) { return this . _zone . runOutsideAngular ( ( ) => { const tempSubj = new Subject < Event > ( ) ; const result = this . _renderer . listen ( target , eventName , ( e ) => { tempSubj . next ( e ) ; e . preventDefault ( ) ; e . stopPropagation ( ) ; } ) ; tempSubj . debounceTime ( debounceTime ) . subscribe ( e => this . _zone . run ( ( ) => listener ( e ) ) ) ; return result ; } ) ; } }