在RxJS中,interval和timeInterval运算符有什么区别?

时间:2019-02-18 10:47:51

标签: rxjs

我试图用Google搜索它,但找不到任何明确的答案。 从文档中,我注意到一个是运算符,一个是函数。

它们之间有什么区别?以及我应该在代码中使用什么?

谢谢!

以下是文档链接:

https://rxjs-dev.firebaseapp.com/api/operators/timeInterval

https://rxjs-dev.firebaseapp.com/api/index/function/interval

1 个答案:

答案 0 :(得分:3)

interval()是一种所谓的Observable创建方法,该方法返回一个Observable,该Observable周期性地发射不断增加的数字序列,并且它们之间具有恒定的延迟。

timeInterval()是一个运算符,基本上用其两次最近发射之间的时间来“标记”其源的每个发射。

主要且可能更明显的区别是使用方式:

range(1, 20).pipe(
  timeInterval(), // `timeInterval()` is an operator
).subscribe(...); // TimeInterval objects

interval(1000).pipe( // `interval()` is a source Observable
).subscribe(...); // 0, 1, 2, ...