Angular 在角度5中的单个可观测值中更新多个值

Angular 在角度5中的单个可观测值中更新多个值,angular,Angular,这个场景看起来很简单,但我想不出来。我有下面的代码来给我一个每秒递减的数字: 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(() =>

这个场景看起来很简单,但我想不出来。我有下面的代码来给我一个每秒递减的数字:

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 ));
}
}

我可以倒计时,但不能在剩余的时间内倒计时。我该怎么办?非常感谢。

如果您试图每秒更新cookie和值倒计时,请使用如下函数:

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;
   }
}

这是行不通的。现在我也没有倒计时了。它在那里干什么?在控制台中,我得到了倒计时和饼干,因为它应该工作。从cookie获取“remainingTime”时,请确保其值是一个数字,然后将其设置为一个数字i did:this.counter=Numberthis.\u cookies.get'remainingTime';但是仍然是一样的,在这样做之后,如果isNaNthis.counter是真的,则将其设置为零