当你移动到角7的另一个部件时,停止间隔是怎样的:
public s: Observable<any>;
this.s = interval(5000)
.pipe(takeWhile(() => this.visitors.length > 0))
.subscribe(() => {});我试图在组件析构函数中停止间隔:
ngOnDestroy() {
this.s.unsubscribe();
}发布于 2019-03-17 06:30:33
你犯了两个错误,让我来描述一下:
subscribe()函数不返回Observable;pipe返回。unsubscribe不会停止间隔。我使用的是setInterval,而不是间隔,例如:
timer: any;
ngOnInit() {
this.timer = setInterval(() => {
// here do whatever you want every 5 seconds
}, 5000);
}并使用clearInterval函数onDestroy;例如:
ngOnDestroy() {
clearInterval(this.timer);
}https://stackoverflow.com/questions/55202289
复制相似问题