CombineLatest没有在rxjs中获得最新价值

利津杜拉伊拉伊

这是代码

const timerOne = timer(1000, 4000).pipe(
  map(x=>`1-${x}-${new Date().getSeconds()}`)
)
//timerTwo emits first value at 2s, then once every 4s
const timerTwo = timer(2000, 4000).pipe(
  map(x=>`2-${x}-${new Date().getSeconds()}`)
);
//timerThree emits first value at 3s, then once every 4s
const timerThree = timer(3000, 4000).pipe(
  map(x=>`3-${x}-${new Date().getSeconds()}`)
);

//when one timer emits, emit the latest values from each timer as an array
const combined = combineLatest(timerOne, timerTwo, timerThree);

const subscribe = combined
.pipe(take(5))
.subscribe(
  ([timerValOne, timerValTwo, timerValThree]) => {   
    console.log(
     ` ${timerValOne},
    ${timerValTwo},
     ${timerValThree}`
    );
  }
);

这是rxjs中combineLatest()的定义

在每个可观察对象发出至少一个值之前,它不会发出初始值。

现在根据上面的定义,输出应为

1-2-56,
2-1-57,
3-0-58

代替

1-0-56,
2-0-57,
3-0-58

因为,我们只将得到的值timerThree到那个时候3秒后观察到的,最新的价值timerOne是2和最新的值timerTwo是1,我失去了一些东西,请大家帮帮忙,谢谢

科斯

您的计时器将首先以[1s,2s,3s]发射,并以4s的速率继续发射。

因此,所有流首次发出的时间是3s

下一个事件combineLatest(当所有流都已发出并且任何流都发出新事件时)为:5s,6s,7s ...

继承人的插图

const timerOne = timer(100, 400);
const timerTwo = timer(200, 400);
const timerThree = timer(300, 400);
combineLatest(timerOne, timerTwo, timerThree, (...arr)=>arr.join('-'));

带Combine的计时器

这是一个将计时器与最新结合的游乐场

希望这可以帮助

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章