首页 > 解决方案 > 如何在角度 6 中取消订阅计时器?

问题描述

我在使用角度 6 中的计时器时遇到问题。在某些满足条件后,我无法取消订阅计时器。

this.testInterval=interval(1000).pipe(
      map(x => this.testSecTime )
    ).subscribe(x => this.reduceTimer());

标签: angular

解决方案


用于unsubscribe此。

如果页面破坏

ngOnDestroy() {
    this.testInterval.unsubscribe()
}

或者

对于任何条件

this.testInterval.unsubscribe()

推荐阅读