Angular 6订阅中未触发rxjs6计时器以触发放置在服务中的可观察计时器

时间:2018-06-27 23:20:54

标签: timer rxjs angular6 rxjs6

我正在尝试将来自rxjs 6的计时器放在angular 6服务中。而且它没有被触发。我没有任何运气就看了文档。这是我的服务代码(仅相关部分:

import { map,flatMap, catchError, take } from 'rxjs/operators';
import { BehaviorSubject, of, throwError,timer, Observable } from "rxjs";

.....

  countdownTimer = new Observable<number>();


  formatCountdownTime(count) {
    const seconds = count % 60 < 10 ? '0' + Math.floor(count % 60) : Math.floor(count % 60);
    /*    if(count <= 1000){
         //set timer to NOT RUNNING state so it updates UI components like the button that depends on the count
          this.contributorManagerService.countdownTimerIsRunning.next(false);
       } */
       return Math.floor(count / 60) + ':' + seconds;
  }

  createCountdownTimerObservable(count){
    this.countdownTimer =  timer(0, 1000);

  }

,这是计时器的消耗。我需要知道时间何时过去,这就是为什么要将第三个函数参数传递给订阅的原因,因为我需要在时间到时启用按钮。

import { map,take } from 'rxjs/operators';


export class CampaignDataComponent implements OnInit, OnDestroy {

 countdownTimerIsRunning = false ;
 count: number;


ngOnInit(){

/* I subscribe to the timer and the 3rd parameter indicates what to do when time has elapsed */
      this.sharedHelpersService.countdownTimer.
      pipe(
        take(this.count),
        map(() => {
          --this.count;

          return this.count;
        })
      ).subscribe(count=>{
        console.log("New count",count);

          if(count>0){
             this.countdownTimerIsRunning = true;
          }
      },
      err=>{
        console.log("Countdown timer error", err);
      },
      ()=>{
        console.log("Time has elapsed");
        this.countdownTimerIsRunning = false;
      });

}

}

您知道为什么不被触发吗?当我在组件上使用整个链时,它曾经可以工作,但是由于我需要从其他组件中使用它,因此不得不将其放入服务中,这使它中断了。有任何想法吗?。预先非常感谢

编辑:为澄清起见,它们所有组件应消耗相同的倒计时

3 个答案:

答案 0 :(得分:1)

问题出在此代码上

image(sortIndex(i,1))

我假设组件中没有其他代码,因此this.count初始化为0。然后,您预订可观察对象并说有效take(0),因此可观察对象立即完成。

答案 1 :(得分:1)

以下方法可以使计时器由多个组件共享:

服务:

import { Injectable } from '@angular/core';
import { timer, Subject, Observable } from 'rxjs';
import { takeWhile, map } from 'rxjs/operators';

@Injectable()
export class CountdownService {

  private _countdown = new Subject<number>();

  countdown(): Observable<number> {
    return this._countdown.asObservable();
  }

  private isCounting = false;

  start(count: number): void {
    // Ensure that only one timer is in progress at any given time.
    if (!this.isCounting) {
      this.isCounting = true;
      timer(0, 1000).pipe(
        takeWhile(t => t < count),
        map(t => count - t)
      ).subscribe(
        t => this._countdown.next(t),
        null,
        () => {
          this._countdown.complete();
          this.isCounting = false;
          // Reset the countdown Subject so that a 
          // countdown can be performed more than once.
          this._countdown = new Subject<number>();
        }
        );
    }
  }
}

组件可以使用

进行倒数计时
countdownService.start(myCountdownTime)

以及所有对倒计时感兴趣的组件都可以订阅

countdownService.countdown().subscribe(t => ...)

Stackblitz Example

答案 2 :(得分:0)

当您说需要使用其他组件的倒计时时,如果您表示需要使用其他组件的功能,但是每个订阅的组件都有各自独立的倒计时,则可以执行以下操作:

服务:

import { timer, Observable } from 'rxjs';
import { filter, takeWhile, map } from 'rxjs/operators';

// startTime is in seconds
count(startTime: number): Observable<number> {
  return timer(0, 1000).pipe(
    takeWhile(t => t < startTime),
    map(t => startTime - t)
  )
}

组件:

// Count down for 10 seconds.
countdownService.count(10).subscribe(
  t => console.log(t), 
  null, 
  () => console.log('Done!')
)

这将打印:10 9 8 7 6 5 4 3 2 1完成!

相关问题