Newsha Nik
Newsha Nik

Reputation: 957

RxJS countdown timer doesn't reach zero

I have a startCountdown() function in my angular app that is basically a 2-minute countdown timer.

Problem is, it doesn't reach zero and stops at 0:01.

startCountdown() function is as follows:

startCountdown(): void {
    const timerInterval$ = interval(1000); //1s
    const timer$ = timer(120000); //120s
    const times = 120;
    const countDown$ = timerInterval$.pipe(take(times));
    const sub = countDown$.subscribe((val) => {
      this.countdownTimer = secondsToTime(times - val);
    });
  }

And this is the secondsToTime() function:

secondsToTime(seconds: number): string {
  const minute: number = Math.floor(seconds / 60);
  const second: number = seconds == 0 ? 0 : seconds % 60;

  if (second < 10) {
    return `${minute}:0${second}`;
  } else {
    return `${minute}:${second}`;
  }
}

Upvotes: 0

Views: 730

Answers (1)

HTN
HTN

Reputation: 3604

Well: the interval(1000).pipe(take(120)) emit 120 values from 0 to 119

Upvotes: 2

Related Questions