[英]How to clearTimeout a setTimeout in another function in React JS?
I have something like this:我有这样的事情:
redTimeout = () => {
setTimeout(() => {
this.props.redBoxScore();
this.setState({
overlayContainer: 'none'
});
}, 5000);
}
And I have a method runs when a button is clicked:我有一个方法在单击按钮时运行:
handleTerminate = () => {
}
How can I stop the setTimeout
in the another function when the button is clicked?单击按钮时如何停止另一个 function 中的
setTimeout
?
Put the timeout ID returned by setTimeout
into state or a ref or something so that you can reference it in the other function:把
setTimeout
返回的timeout ID放到state或者一个ref什么的,这样就可以在另一个function中引用了:
redTimeout = () => {
const timeoutId = setTimeout(() => {
this.props.redBoxScore();
this.setState({
overlayContainer: 'none'
});
}, 5000);
this.setState({ timeoutId });
}
handleTerminate = () => {
clearTimeout(this.state.timeoutId);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.