Javascript 更改超时持续时间

Javascript 更改超时持续时间,javascript,angular,typescript,rxjs,Javascript,Angular,Typescript,Rxjs,我正在构建Angular 6应用程序,其中我有多个项目,每个项目都有一些配置。在每个项目上,我定义了该项目必须可见的持续时间 我创建了这个,但我感兴趣的是它是否可以用rxjs编写,或者是否有更简单的解决方案 const timer = () => { this.counter++; this.currentItem = this.items[this.counter % this.items.length]; setTimeout(timer, this.curre

我正在构建Angular 6应用程序,其中我有多个项目,每个项目都有一些配置。在每个项目上,我定义了该项目必须可见的持续时间

我创建了这个,但我感兴趣的是它是否可以用rxjs编写,或者是否有更简单的解决方案

const timer = () => {
    this.counter++;
    this.currentItem = this.items[this.counter % this.items.length];
    setTimeout(timer, this.currentItem.duration);
};
setTimeout(timer, this.currentItem.duration);

使用RxJS 6,您可以执行类似的操作

from(items)
  .pipe(
    concatMap(item => of(null) // Wait until the previous inner observable completes
     .pipe(
       delay(item.duration), // Delay emission after this value
       ignoreElements(),
       startWith(item),
     )
    ),
    repeat(),
  )
  .subscribe(console.log);

请参阅现场演示:

太好了。如何在循环中执行此操作?当最后一个项目完成时,再次转到第一个元素。因此当前项目的长度不会很长,例如1000毫秒,但下一个项目的持续时间设置会很长。不为当前项目设置延迟。当我登录到控制台时,第二个项目有第三个元素的延迟,它只显示1秒。那么你想怎么做?先记录项目,然后进行延迟,还是先进行延迟,然后记录项目?