Javascript 如何在 Angular2 中停止 observable.timer
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44039278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Stop observable.timer in Angular2
提问by xKxAxKx
I am implementing the following functions in Angular2's Component:
我正在 Angular2 的组件中实现以下功能:
export class MypageEditComponent {
ngOnInit() {
this.timer = Observable.timer(100, 100);
this.timer.subscribe(t => {
this.setFormData();
}
private setFormData() {
this.editUserAcountType = this.authStore.registerInfo.account_type;
this.editAddress = this.authStore.registerInfo.email;
this.editUserName = this.authStore.registerInfo.username;
}
}
I want to stop the repeat of Observable.timeronce the value is correctly stored with setFormData().
Observable.timer一旦值被正确存储,我想停止重复setFormData()。
But do not know how, please tell me.
但不知道如何,请告诉我。
回答by martin
There're are basically two ways:
基本上有两种方式:
- call
unsubscribe()on the Subscription object returned from thesubscribe()call . - use an operator
- 调用
unsubscribe()从subscribe()调用返回的订阅对象。 - 使用运算符
To just unsubscribeyou could do it like this.
只是unsubscribe你可以这样做。
ngOnInit() {
this.subscription = timer(100, 100).subscribe(t => {
this.setFormData();
});
}
private setFormData() {
...
this.subscription.unsubscribe();
}
Or you can use Subject to complete the Observable via takeUntil()operator:
或者你可以使用 Subject 通过takeUntil()操作符来完成 Observable :
this.subject = new Subject();
ngOnInit() {
timer(100, 100).pipe(
takeUntil(this.subject),
).subscribe(t => this.setFormData());
}
private setFormData() {
...
this.subject.next();
}
Have a look these as well:
看看这些:
Jan 2019: Updated for RxJS 6
2019 年 1 月:针对 RxJS 6 更新
回答by Pardeep Jain
you can use unsubscribemethod when you want to stop timer observable like this
unsubscribe当您想像这样停止可观察的计时器时,您可以使用方法
this.timer = Observable.timer(100, 100);
this.subscription = this.timer.subscribe(t => {
this.setFormData();
});
.............
this.subscription.unsubscribe();
回答by controlbox
A nuance which previous answers haven't really covered is that there is no need to stopan observable timer/interval - at least not by interacting with the observable or the timer itself.
以前的答案没有真正涵盖的一个细微差别是,不需要停止可观察的计时器/间隔 - 至少不需要与可观察的或计时器本身进行交互。
I found myself here because I was setting up an observable using a timer in a HostedService's start method, and in the stop method it seemedlike I should be stopping it, or disposing of something surely? - well... no. The observable timer does not need to be stopped or disposed in this way, only the subscriptions, and this is why...
我发现自己在这里是因为我在 HostedService 的 start 方法中使用计时器设置了一个 observable,而在 stop 方法中,我似乎应该停止它,或者肯定地处理一些东西?- 嗯……不。observable timer 不需要以这种方式停止或处理,只需要订阅,这就是为什么......
Observable timers and intervals are examples of cold streams(though they appear to behave as hot). They are passive not active, they do not produce data till subscribed to, and stop producing data when no subscribers. Therefore, disposal of all subscriptions should be all that's required to stop the timer producing data.
可观察的计时器和间隔是冷流的例子(尽管它们看起来像热流)。它们是被动的而不是主动的,它们在订阅之前不会产生数据,并且在没有订阅者时停止产生数据。因此,处理所有订阅应该是停止计时器生成数据所需的全部内容。
More info here... [http://introtorx.com/Content/v1.0.10621.0/14_HotAndColdObservables.html]
更多信息在这里... [ http://introtorx.com/Content/v1.0.10621.0/14_HotAndColdObservables.html]
回答by Alexander J. Mercer
xD did you know you can make it a promise?
xD 你知道你可以做出承诺吗?
import { timer } from 'rxjs';
timer(1000).toPromise().then(res => {
//code here
})
no need to deal with unsubscribe.
无需处理退订。
回答by Muhammed Moussa
-- workaround
-- 解决方法
setTimeout(() => observable.next(null) , 2000);

