我想创建一个基于Observable的轮询器,它在前一个请求返回和下一个请求发出之间等待一段时间。
这是我尝试过的代码,但这会在请求之间设置延迟:
import {timer} from "rxjs";
this.timer = timer(1, POLLING_INTERVAL)
.pipe(concatMap(
(_) => getData()
)).subscribe((data) => {
// do something with data
});
timer
不是理想的。而是使用repeatWhen
和delay
。
import { of } from 'rxjs';
import { repeatWhen, delay } from 'rxjs/operators';
getData().pipe(
repeatWhen(notifications => notifications.pipe(
delay(POLLING_INTERVAL),
)),
).subscribe(...);
您必须使用以下创建间隔:https://stackblitz.com/edit/typescript-ohddud?file=index.ts&devtoolsheight=100或带有两个参数的计时器:https://stackblitz.com/edit/typescript-h9pzxr?file=index.ts&devtoolsheight=100
我希望你以正确的方式合并请求。