我想每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() {
...
}
}