How to destroy/stop observable interval
you must have created a subscrtiption based on your this.countDown
, you can destroy it in ngOnDestroy
,
Excerpt from documentation,
Put cleanup logic in ngOnDestroy(), the logic that must run before Angular destroys the directive.
....
this.subscription = this.countDown.subscribe(...)
...
public ngOnDestroy() {
this.subscription.unsubscribe();
}
You can use takeUntil
operator.
import { Subject } from 'rxjs/Subject';
import 'rxjs/add/operator/takeUntil';
...
private onDestroy$ = new Subject<void>();
ngOnDestroy(): void {
this.onDestroy$.next();
}
ngOnInit() {
this.countDown = Observable.interval(1000)
.takeUntil(this.onDestroy$) // <----
.map(
...