Reputation: 7240
The scenario seems simple but i could not figure it out. I have the following code to give me a decrementing number per second:
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 ));
}
}
I can get countDown
decremented but could not do it for the cookie remainingTime
. What should i do? Many thanks in advance.
Upvotes: 0
Views: 116
Reputation: 847
If you're trying to update the cookie and the value countDown every second, then do it in a function like this:
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;
}
}
Upvotes: 1