Observable - 在一个轮询返回和另一个轮询开始之间设置常量延迟

问题描述 投票:0回答:2

我想创建一个基于Observable的轮询器,它在前一个请求返回和下一个请求发出之间等待一段时间。

这是我尝试过的代码,但这会在请求之间设置延迟:

import {timer} from "rxjs";

    this.timer = timer(1, POLLING_INTERVAL)
     .pipe(concatMap(
        (_) => getData()
      )).subscribe((data) => {
        // do something with data     
      });

rxjs rxjs6
2个回答
1
投票

timer不是理想的。而是使用repeatWhendelay

import { of } from 'rxjs';
import { repeatWhen, delay } from 'rxjs/operators';

getData().pipe(
  repeatWhen(notifications => notifications.pipe(
    delay(POLLING_INTERVAL),
  )),
).subscribe(...);

现场演示:https://stackblitz.com/edit/rxjs-2evzzi


0
投票

您必须使用以下创建间隔:https://stackblitz.com/edit/typescript-ohddud?file=index.ts&devtoolsheight=100或带有两个参数的计时器:https://stackblitz.com/edit/typescript-h9pzxr?file=index.ts&devtoolsheight=100

我希望你以正确的方式合并请求。

© www.soinside.com 2019 - 2024. All rights reserved.