如果我不在应用程序中,请延迟setInterval?

时间:2019-06-10 19:30:55

标签: javascript reactjs redux create-react-app

我想每5分钟获取一些信息,我的代码可以工作,但是如果我打开其他程序,我认为setInterval处于休眠状态,有没有办法保持活动状态?

class App extends Component {

  state = {
    minutes: 5,
    seconds: null
  }

  componentDidMount() {
    this.interval = setInterval(() => this.tickingForFetch(), 1000)
  }

  componentWillMount() {
    this.setState((prevState) => {
      return {
        seconds: prevState.minutes * 60
      }
    })
  }


  tickingForFetch = () => {
    const { dispatch } = this.props
    const { minutes, seconds: secondsLeft } = this.state

    if (minutes * 60 === secondsLeft) {
      dispatch(fetchInfo())
    }
    else if (secondsLeft === 0) {
      this.setState({
        seconds: minutes * 60
      })
      return
    }

    this.setState({
      seconds: secondsLeft - 1
    })
  }

  render() {
    ...
  }

}

0 个答案:

没有答案