【问题标题】:How to call an API with certain time interval in Angular 4如何在Angular 4中以特定时间间隔调用API
【发布时间】:2018-10-30 22:59:35
【问题描述】:

我正在使用 Angular 4 应用程序,在此我需要从我的服务文件中调用 API。

这里我每隔15分钟调用一次服务文件中的方法。但是它显示错误如下

但是如果我在不传递参数的情况下调用 API,它工作正常,当我从服务调用将参数传递给 API 的方法时,它不会被触发。

服务方式

update_User_Session() {
    this.publicIp.v4().then(ip => {
      this.END_USER_SESSION(ip)
    })

    this.publicIp.v4().then(ip => {
      this.INSERT_USER_SESSION(ip)
    })
  }

  INSERT_USER_SESSION(address: string) {
    this.http.get(`http://localhost:xyz/api/data/INSERT_USER_SESSION/?IP_Address=${address}&Time=${this.date_and_time}&state=${this.session_Begin_Status}`)
  }

  END_USER_SESSION(address: string) {
    this.http.get(`http://localhost:xyz/api/data/INSERT_END_USER_SESSION/?Ip_Address=${address}&Time=${this.date_and_time}&state=${this.session_End_Status}`)
  }

来自我的组件

constructor(private CartdataService: CartdataService, private http: HttpClient) {

        this.call = Observable.interval(90000)
            .switchMap(() => this.CartdataService.update_User_Session())
            .subscribe((data) => {
                console.log(data);
            });
    }

这行.switchMap(() => this.CartdataService.update_User_Session()) 变成了红线,并显示了上述错误,如图所示

谁能告诉我,我在哪里做错了。

【问题讨论】:

  • 你的update_User_Session 应该返回Observable,但你返回null

标签: angular


【解决方案1】:
//use retryWhen from rxjs like below
//first  import retryWhen and delay operators in your service file.
//use before subscribe like below example as i given.

import 'rxjs/add/operator/retrywhen';
import 'rxjs/add/operator/delay';

ngOnInit() {
    let empCode: string = this._activatedRoute.snapshot.params['code'];

    this._employeeService.getEmployeeByCode(empCode)
        // Retry with a delay of 1000 milliseconds (i.e 1 second)
        .retryWhen((err) => err.delay(1000))
        .subscribe((employeeData) => {
            if (employeeData == null) {
                this.statusMessage =
                    'Employee with the specified Employee Code does not exist';
            }
            else {
                this.employee = employeeData;
            }
        },
        (error) => {
            this.statusMessage =
                'Problem with the service. Please try again after sometime';
            console.error(error);
        });
}

【讨论】:

  • 你能用你的逻辑编辑我的代码吗@mittalbhatt
【解决方案2】:
Use observable.timer for your purpose in angular way.

  private timer;

constructor(private CartdataService: CartdataService, private http: HttpClient) {
}


  ngOnInit() {
    this.timer = Observable.timer(9000);
    this.timer.subscribe((t) => this.onTimeOut());
  }
   onTimeOut() {
    this.CartdataService.update_User_Session().then(
    success => {
    if(success ['ok'] == 0){
      console.log('test');
    }
    },
    error => { console.log(error); });
}

   ngOnDestroy(){
    console.log("Destroy timer");

  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-09
    • 2015-04-24
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    相关资源
    最近更新 更多