带有setInterval的RxJS退订

时间:2018-08-06 14:56:19

标签: angular rxjs

如何从该示例中调用取消订阅方法?

var observable = Rx.Observable.create(function subscribe(observer) {
 // Keep track of the interval resource
  var intervalID = setInterval(() => {
    observer.next('hi');
  }, 1000);

  // Provide a way of canceling and disposing the interval resource
  return function unsubscribe() {
  clearInterval(intervalID);
  };
});

1 个答案:

答案 0 :(得分:0)

你能这样尝试吗?

let subscription = observable.subscribe(sub=> {
    // call unsubscribe
    subscription.unsubscribe();
});