代码:
myMethod = () => {
setTimeout(()=> {
if(!this.state.otherFuncHasBeenCalled) {
this.myMethod()
}
},5000)
}
如果我的其他函数被调用,则otherFuncHasBeenCalled
状态设置为true。但我想在这里做的是在调用myMethod
时,在5秒之后,如果未调用我的其他函数将otherFuncHasBeenCalled
状态设置为true,则再次调用myMethod
。但它会多次渲染组件。帮助
答案 0 :(得分:0)
这样的事情怎么样?
myMethod = () => {
setTimeout(()=> {
if(!this.state.otherFuncHasBeenCalled) { // wait 5 sec for check
this.setState(
{otherFuncHasBeenCalled: true}, // update state
this.myMethod // run again after state has been set
}
},5000)
}