【发布时间】:2018-09-21 20:41:21
【问题描述】:
在每个问题组件上,我都在尝试清除超时。所以在componentWillMount() 我将启动计时器,然后在componentDidUpdate() 我将清除超时。计时器似乎不起作用。计时器到期后,我会将用户推回主页。知道为什么使用 clearTimeout() 不起作用吗?
class Questions extends Component {
constructor(props){
super(props);
this.state ={
error: false,
position: null,
redirect: false
}
this.error = this.error.bind(this);
this.errorFalse = this.errorFalse.bind(this);
this.timer = this.timer.bind(this);
}
timer=()=>{
setTimeout(() => {
console.log('this ran')
this.setState({
redirect: true
})
}, 5000)
}
componentWillMount(){
this.setState({
error: false
})
this.timer();
}
componentDidUpdate(prevProps, prevState, snapshot, timer){
console.log('updated')
clearTimeout(this.timer);
}
【问题讨论】:
标签: javascript reactjs settimeout cleartimeout