我创建了一个 API 调用,使用以下代码可以正常工作:
timeoutTime : number = 10000;
this.http.post(this.apiUrl+'/login', JSON.stringify(data), {
headers: new HttpHeaders().set('Content-Type', 'application/json').set('language', this.translate.getDefaultLang()),
}).pipe(timeout(this.timeoutTime))
.subscribe(res => {
resolve(res);
}, (err) => {
if (err.status == 408 || err.status == 0 || err.status == 503 || err.name == 'TimeoutError'){
reject(this.getTimeoutResponse());
} else {
}
});
});
}
问题是:
那部分不起作用
.pipe(timeout(this.timeoutTime))
未设置超时。 这是一个使用 Ionic 的 Angular 应用程序,默认超时接近 5 秒,我正在尝试将其更改为 10 秒。
谢谢
根据文档:
timeout
如果 Observable 在给定时间范围内未发出值,则会出现错误。
用更清楚的话来说:Observable 的超时不能足够快地发出值。 https://rxjs.dev/api/index/function/timeout
您正在寻找的是:
timer
创建一个可观察对象,它将在发出之前等待指定的时间段或确切的日期。它用于在延迟后发出通知。
https://rxjs.dev/api/index/function/timer
所以更正后的代码是:
.pipe(timer(this.timeoutTime))