简体   繁体   English

如何在 Angular2 中停止 observable.timer

[英]How to Stop observable.timer in Angular2

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.timer once the value is correctly stored with setFormData() .一旦使用setFormData()正确存储值,我想停止Observable.timer的重复。

But do not know how, please tell me.但不知道如何,请告诉我。

There're are basically two ways:基本上有两种方式:

  • call unsubscribe() on the Subscription object returned from the subscribe() call .对从subscribe()调用返回的 Subscription 对象调用unsubscribe()
  • use an operator使用运算符

To just unsubscribe you 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 更新

you can use unsubscribe method 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();

A nuance which previous answers haven't really covered is that there is no need to stop an 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 seemed like I should be stopping it, or disposing of something surely?我发现自己在这里是因为我在 HostedService 的 start 方法中使用计时器设置了一个 observable,而在 stop 方法中,我似乎应该停止它,或者肯定地处理一些东西? - well... no. - 嗯……不。 The observable timer does not need to be stopped or disposed in this way, only the subscriptions, and this is why... 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]

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.无需处理退订。

-- 解决方法

setTimeout(() => observable.next(null) , 2000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM