本文介绍了如何更改订阅的价值?使用rxJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建计时器,需要您的帮助我只是在学习角度&rxJS和我对此有疑问
im creating a timer, and need your helpim just learning angular & rxJS and i have some question about this
我正在创建一个具有启动,停止,暂停,重置的计时器
Im creating a timer which have start,stop,pause,reset
并且btn重置必须将我的计时器暂停"到300ms
and btn reset must 'pause' my timer to 300ms
该怎么做?:D
我的启动计时器fnc
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;
});
}
从此开始,我有了
pause() {}
不知道该怎么做:D
推荐答案
我创建了一个具有停止恢复和暂停功能的计时器 https://stackblitz.com/edit/angular-gq9zvk .
I create an timer with stop resume and pause https://stackblitz.com/edit/angular-gq9zvk.
您的变量
isstop = new Subject();
ispause = new Subject();
private time = 0;
isRunning = true;
timer: Observable<number>;
timerObserver: PartialObserver<number>;
此设置的计时器置于onInit或连接到单击启动功能
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();
}
这篇关于如何更改订阅的价值?使用rxJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!