停止Angular2倒数计时 [英] Stop Angular2 CountDown
本文介绍了停止Angular2倒数计时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想停止我的倒计时
,但是它没有停止并且仍在运行,我有这个:
I'd like to stop my Countdown
but it does not stops and still running I have this :
import { Subscription } from 'rxjs/Subscription';
sub: Subscription;
countDown;
count;
this.count = 100;
this.countDown = Observable.timer(0, 1000)
.take(this.count)
.map(() => this.count = this.count-10);
this.sub = Observable.interval(1000)
.subscribe((val) => {
if(this.count===0){
this.sub.unsubscribe();
}
});
当计时器等于0时,它变为-10,依此类推...
and when the timer equals to 0 it goes to -10, etc...
还有另一种方法可以停止计时器吗?
Is there another way to stop that timer?
推荐答案
我最终创建了一个称为 stopTimer()
的函数,即:
I ended up creating a function called stopTimer()
that is :
stopTimer(){
this.countDown = null;
this.sub.unsubscribe();
}
它将停止 Timer
并停止 CountDown
。
这篇关于停止Angular2倒数计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文