setTimeout写轮询

 代码如下(每个几秒发送请求,刷新数据)

  private startTimer(autoRefreshTimeS: number) {
    if (autoRefreshTimeS < 0) {
      this.stopTimer();
      return;
    }
    const timer = setTimeout(() => {
      this.fetchImagesThenAssignToState();
      this.startTimer(autoRefreshTimeS);
    }, autoRefreshTimeS * 1000);
    this.setState({timer});
  }

这里主要是react+typescript语法,主要逻辑是setTimeout中调用获取数据函数,然后再调用自身

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章