Creating an observable that emits values periodically (error: observer.timer is not a function)

Creating an observable that emits values periodically (error: observer.timer is not a function)

我有一个看起来像这样的对象:[{ timestamp: object }]
timestamp 已创建 timestamp = Date.now();

现在,我正在寻找一种方法来创建一个 Observable 以真实顺序发射对象,即当条目之间的时间为 200 毫秒时,则需要等待 200 毫秒,如果为 2.5 秒,则它需要等待 2500ms.

我通过将两个相邻索引值彼此相减得到差值。

我的代码在这里:

startReplay() {
  this.recordingRunning = true;
  const replayValues = this.recordedListeningValues;
  this.formObservable = Observable.create(function(observer) {
    let lastTs;
    Object.keys(replayValues).forEach(function(key) {
        observer.timer(Math.floor((Number(key) - lastTs) / 1000)).next(key, lastTs);
        lastTs = Number(key);
    });
  });
  var source = this.formObservable.subscribe(x => console.log(x));
}

它抛出错误:observer.timer is not a function

目前,我只想 log 以秒为单位的时间戳之间的差异。现在,我想在两个时间戳之间的差异中发出它。

您可以使用 delayWhen 运算符。但是你需要知道记录开始的时间戳,这样你就可以计算出每个事件相对于记录开始的时间

const startOfRecording = ??? // Date.now() when recording started
const recordedValues = ???

// each time someone subscribes to this observable
// the recorded events will play back in "real time"
this.formObservable = Observable
    .from(Object.keys(replayValues))
    .delayWhen(key => Observable.timer(key - startOfRecording))
    .map(key => replayValues[key]);