I wanted to reuse my working jQuery
function in Angular which has two functions, one called on keyup
other on keydown
<input type="text" [(ngModel)]="filterOptions.SearchValue" (keyup)="onFilter($event)" (keydown)="resetCounter()" class="form-control">
So that the counter gets reset every time on keydown
. However this doesn't work:
timer = 0;
onFilter(event: any) {
if (!HelperService.keypressFilter(event))
return;
console.log(this.timer);
clearTimeout(this.timer);
this.timer = setTimeout(this.resetValues(), 2000);
}
resetCounter(){
console.log(this.timer);
clearTimeout(this.timer);
console.log(this.timer);
}
Each of the 3 console logs write the same number in the console, and they do so incrementally (i.e. 0 0 0, 3 3 3, 24 24 24...) after each key press. Those numbers have nothing to do with milliseconds. How can I make it work?