Angular setInterval错误

时间:2018-02-01 10:32:31

标签: javascript angular typescript

我想为一个页面设置轮询,并在用户退出此页面时将其禁用。但是我在构建期间遇到错误,当我调用函数setInterval进行轮询时:

error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.

这是班级:

export class CommandPageComponent implements OnInit, OnDestroy {

  private lastUpdate: number;
  private timer: any;
  private pollingTimer: any;
  private seconds = 0;

  constructor(private router: Router, private consultService: ConsultService, private configService: ConfigService) {

    this.configService.getConfigData('pollingEnabled').subscribe(enabled => {
      if (enabled) {
        let j = 0;
        this.configService.getConfigData('pollingInterval').subscribe(interval => {
          this.pollingTimer = setInterval(() => {
            console.log('polling', ++j);
            console.log('polling', interval);
            this.seconds = 0;
            this.consultService.sendConsultPollingRequest();
          }, interval * 1000);
          this.timer = setInterval(() => {
            this.lastUpdate = interval - this.seconds++;
          }, 1000);
        });
      }else {
        console.log('Polling disabled');
      }
    });
  }


  ngOnDestroy() {
    // Will clear when component is destroyed e.g. route is navigated away from
    if (this.pollingTimer) {
      clearInterval(this.pollingTimer);
    }
    if (this.timer) {
      clearInterval(this.timer);
    }
    console.log('Clear polling');
  }

}

你知道问题出在哪里吗?谢谢。

1 个答案:

答案 0 :(得分:0)

问题非常明确:

this.configService.getConfigData('pollingInterval').subscribe(interval => {

您的interval值不是number,因为这两行

interval * 1000

this.lastUpdate = interval - this.seconds++;

抛出错误

  

错误TS2362:算术运算的左侧必须是   输入'any','number'或枚举类型。