Trevor Hector
Trevor Hector

Reputation: 639

Angular timer only on mousedown

I want to add a timer initiated through mousedown

let timer = Observable.timer(2000,1000);
timer.subscribe(t=> this.ticks());

I want to stop the timer as soon as mouseup get's fired. The event target is an image:

<img [src]="source"  (mousedown)="activateTimer()" (mouseup)="deactivateTimer()">

Do I need to create the timer each event cycle in the activateTimer() and remove it and the deactiveTimer()?

Is there a solution where all is one stream?

Upvotes: 0

Views: 126

Answers (1)

Thien Hoang
Thien Hoang

Reputation: 625

You can do with this approach

isPause: boolean = false;

constructor() {
    let timer = Observable.interval(1000);
    timer
       .filter(number => !this.isPause)
       .subscribe(t=> this.ticks());
}

activateTimer() {
   this.isPause = false;
}

deactivateTimer() {
   this.isPause = true;
}

Upvotes: 1

Related Questions