这个场景看起来很简单,但我无法弄清楚。我有以下代码可以给我一个每秒递减的数字:
counter: number;
countDown: number;
ngOnInit(){
this.counter = this._cookies.get('remainingTime');
if(this.counter > 0){
this.countDown = Observable.timer(0,1000)
.take(this.counter)
.map(() => --this.counter );
this._cookies.set('remainingTime', () => Observable.timer(0,1000)
.take(this.counter)
.map(() => --this.counter ));
}
}
我可以countDown
递减,但不能为 cookie 做remainingTime
。我应该怎么办?提前谢谢了。
如果您尝试每秒更新 cookie 和 countDown 值,请在如下函数中执行此操作:
timer: any;
timerSubscription: any;
ngOnInit(){
this.counter = this._cookies.get('remainingTime');
this.countdDown = counter;
if(this.counter > 0){
this.timer = Observable.timer(0, 1000);
this.timerSubscription = timer.subscribe(t => this.timerFunction());
}
}
timerFunction() {
this.countDown = this.countDown - 1;
this._cookies.set('remainingTime', this.countDown);
if (this.countDown === 0) {
this.timerSubscription.unsubscribe();
this.timer = null;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句