angular - 多次点击 URL 最多 1 分钟
问题描述
我有一个角度函数,它返回状态为“继续”或“完成”的数据集。我想继续拨打电话,直到状态返回“完成”,但最多只能持续一分钟。我怎样才能在角度实现这一点
public getLeadsResponse(key: any) {
this._service.getLeadsData(key).subscribe((response: any) => {
if (response) {
if (response.payload.status == "running") {
// here if i repeate this function it will start an infinite loop if status is always "running"
this.getLeadsResponse(key);
}
else if (response.payload.status == "finished") {
this.items = res.payload.result;
}
}
})
}
解决方案
您可以使用expand()
:
import { of, EMPTY, timer } from 'rxjs';
import { expand, map, filter, takeUntil } from 'rxjs/operators';
const makeHttpRequest$ = timer(1000).pipe(map(() => Math.random() > 0.9 ? 'finished' : 'continue'));
const source = of(null).pipe(
expand(value => value === 'finished' ? EMPTY : makeHttpRequest$),
filter(value => value !== null), // Ignore the first `of(null)` that triggered the chain.
takeUntil(timer(60 * 1000)),
);
source.subscribe(console.log);
现场演示:https ://stackblitz.com/edit/rxjs-8n11jj?devtoolsheight=60
推荐阅读
- c# - 在 View 中访问 ViewBag 变量
- html - 滚动定位在不同缩放级别下表现不同
- sql-server - 是否有 CREATE TABLE 或 CREATE PROCEDURE 自动确定 SSMS 中的列类型?
- amazon-web-services - AWS 中的多区域 Cassandra 仅在“nodetool 状态”上显示本地
- javascript - Why does two recaptcha appear at one moment in time?
- sql-server - 获取大于 SSRS 中当前日期的所有日期
- javascript - webpack.config 的问题
- awk - 创建包含总数和百分比的 bin
- java - 如何触发 Java 单元测试的 JsonProcessingException?
- string - 如何将此数字和符号列表转换为字符串?