http请求超时函数未根据经过的时间执行

fnvucqvd  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(382)

我遵循这个解决方案在对远程服务器的http调用中实现超时。
这是调用函数;如果请求返回null,则警报将进入视图:

  1. this.service.getAll().subscribe(response => {
  2. console.log("home.page: calling servuce for data...data returned is : " + response);
  3. if (response != null ){
  4. this.tests = response;
  5. this.searching = false; //Turn spinner off
  6. }else{
  7. //http timeout after 2000ms
  8. this.alertCtrl.create({
  9. header: "Connection Timeout",
  10. message: "Check your internet connection!",
  11. buttons: [
  12. {
  13. text: "Ok"
  14. }
  15. ]
  16. }).then(alert => alert.present());

这是http服务;如果http调用在10秒(1000毫秒)后超时,则会将null返回给调用方(如上所示):

  1. import { timeout, catchError } from 'rxjs/operators';
  2. import { of } from 'rxjs/observable/of';
  3. getAll(){
  4. //return this.http.get<[Student]>(this.url);
  5. console.log("student.service-59- GET ALL called..etunr null if times out after 10000ms");
  6. return this.http.get<[Test]>(this.url)
  7. .pipe(
  8. timeout(10000),
  9. catchError(e => {
  10. console.log("student service GET ALL timed out, retunr null, error = ");
  11. return of(null);
  12. })
  13. )
  14. }

但是,当我在internet连接关闭的情况下进行测试时,警报/提示会立即出现,并且不会出现超时参数中定义的等待10秒的情况。
你知道为什么会这样吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题