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

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

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

this.service.getAll().subscribe(response => {
      console.log("home.page: calling servuce for data...data returned is : " + response);
      if (response != null ){
        this.tests = response;
        this.searching = false; //Turn spinner off
      }else{
        //http timeout after 2000ms
        this.alertCtrl.create({
          header: "Connection Timeout",
          message: "Check your internet connection!",
          buttons: [
            {
            text: "Ok"
            }
        ]

        }).then(alert => alert.present());

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

import { timeout, catchError } from 'rxjs/operators';
import { of } from 'rxjs/observable/of';

    getAll(){
        //return this.http.get<[Student]>(this.url);
        console.log("student.service-59- GET ALL called..etunr null if times out after 10000ms");
        return this.http.get<[Test]>(this.url)
          .pipe(
            timeout(10000),
            catchError(e => {
              console.log("student service  GET ALL timed out, retunr null, error = ");
              return of(null);

            })
          )

      }

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

暂无答案!

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

相关问题