I'm using a slightly adjusted version of the debounce pipe shown here: https://github.com/numsu/angular2-debounce/blob/master/src/debounce.directive.ts
The code in the link above updates the model as well, which I didn't need. So pick your poison. My code is:
import { Input, Output } from "@angular/core";
import { EventEmitter, ElementRef, OnInit, Directive } from "@angular/core";
import { Observable } from "rxjs";
@Directive({
selector: '[debounce]'
})
export class DebounceDirective implements OnInit {
@Input() debounceDelay: number = 700;
@Input() debounceFromEvent: string = "tap";
@Output() debounceFunction: EventEmitter<any> = new EventEmitter();
constructor(private elementRef: ElementRef) {
}
ngOnInit(): void {
const eventStream = Observable
.fromEvent(this.elementRef.nativeElement, this.debounceFromEvent)
.debounceTime(this.debounceDelay);
eventStream.subscribe(input => this.debounceFunction.emit(input));
}
}
And you can use it as this on a Label
or whatever input you fancy:
<Label debounce debounceDelay="700" (debounceFunction)="saveChanges()" (tap)="updateStock(selectedArticle.article, 1)" text="Update"></Label>