问题
im creating a timer, and need your help im just learning angular & rxJS and i have some question about this
Im creating a timer which have start,stop,pause,reset
and btn reset must 'pause' my timer to 300ms
how to do it? :D
my start timer fnc
startTimer() {
this.intervalStream$ = interval(1000);
this.sub = this.intervalStream$
.subscribe((value) => {
this.subscribeTimer = this.convertSeconds(this.timeLeft - value);
this.currentTime = this.timeLeft - value;
});
}
and after this start i have
pause() {}
can't understand how to do it :D
回答1:
I create an timer with stop resume and pause https://stackblitz.com/edit/angular-gq9zvk.
Your variables
isstop = new Subject();
ispause = new Subject();
private time = 0;
isRunning = true;
timer: Observable<number>;
timerObserver: PartialObserver<number>;
This set timer put in in onInit or connect to click start function
this.timer = interval(1000)
.pipe(
takeUntil(this.ispause),
takeUntil(this.isstop)
);
this.timerObserver = {
next: (_: number) => {
this.time += 1;
}
};
this.timer.subscribe(this.timerObserver);
and these are stop resume and pause functions
goOn() {
this.isRunning = true;
this.timer.subscribe(this.timerObserver);
}
pauseClick() {
this.ispause.next();
this.isRunning = false;
}
stopClick() {
this.time = 0;
this.isRunning = false;
this.isstop.next();
}
来源:https://stackoverflow.com/questions/61217441/how-to-change-value-in-subscription-using-rxjs