定时器刷新机制 setInterval react

 

在状态初始化时设定一个state{timer:null}

在componentDidMount方法中设置定时执行的方法

this.state.timer=setInterval(()=>{

//需要定时执行的方法

}, 5000)

在componentWillUnmount方法中要对定时器进行销毁

if(this.state.timer!= null) {

clearInterval(this.state.timer);

}

 

posted @ 2019-11-22 13:49  薄荷+猫  阅读(2383)  评论(0编辑  收藏  举报