Michael Joshua Michael Joshua - 1 year ago 76
TypeScript Question

How to check if observable countdown timer has completed?

I have a simple countdown timer in angular 4

import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/timer';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/take';

countDown;
counter = 60;
enableButton:boolean = false;

this.countDown = Observable.timer(0,1000).take(this.counter).map(() => --
this.counter);


I want to make enableButton boolean true when this observable has been completed.

How should I write that code? Please Help

Here is a plunker link, which I am using in my project
https://plnkr.co/edit/TWMIEd1UykUdUC37qyFK?p=preview

Answer Source

In the view:

    <button [disabled]="counter !== 0">OK</button>

and in the class:

 countDown;
  counter = 10;
  isTime=1000;
  constructor() {

    this.countDown = Observable.timer(0,1000)
      .take(this.counter)
      .map(() => --this.counter)

  }

and the plunker

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download