gpt4 book ai didi

angular - 可观察的轮询?

转载 作者:太空狗 更新时间:2023-10-29 17:29:57 24 4
gpt4 key购买 nike

我目前有一个 Observable 计时器:

private poller(): Observable<PrepareData> {
return Observable.timer(0, 5000).switchMap(() => this.http.get("/cgi/dashboard.php")).map(res => res.json());
}

我想要它,以便在最后一个完成后 5 秒完成一个 get 请求。有什么简单的方法可以做到这一点吗?

最佳答案

我将添加我的答案,因为@Maxime 的答案有效,但实际上有 no need for a subject .

这是使用 .concat() 运算符和 getData() 函数递归完成的。

.concat()

Concatenates multiple Observables together by sequentially emitting their values, one Observable after the other.

(...)

concat will subscribe to first input Observable and emit all its values, without changing or affecting them in any way. When that Observable completes, it will subscribe to then next Observable passed and, again, emit its values. This will be repeated, until the operator runs out of Observables. When last input Observable completes, concat will complete as well.

(我使用 js 版本是为了制作一个可以与 stack Overflow 工具一起使用的代码片段,但这对于 typescript 是一样的):

function someHTTPCall() {
// well it's a fake http call
console.log("making the Http request (can take up to 5s)...");
return Rx.Observable.of("http response !")
.delay(Math.round(Math.random() * 5000));
}

function getData() {
return someHTTPCall()
.concat(Rx.Observable.timer(5000).switchMap(() => getData()));
}

let myObs = getData();

myObs.subscribe((data) => {
console.log(data,"waiting for 5 seconds before next request");
});
<script src="https://unpkg.com/rxjs@5.4.0/bundles/Rx.min.js"></script>

关于angular - 可观察的轮询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42657380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com