[英]When I'm running my program I'm getting out of memory error
So, I actually wanna create a timer which will count back time, but when I'm running this script I'm always getting the 'Out of memory' error.所以,我实际上想创建一个可以倒计时的计时器,但是当我运行这个脚本时,我总是遇到“内存不足”错误。 Here is my onClick script:
这是我的 onClick 脚本:
handleClickStart = () => {
while (this.state.hours > 0) {
this.setState({
hours: this.state.hours - 1
})
}
}
Also I could show you the whole code if you'd need it如果您需要,我也可以向您展示整个代码
setState
is asynchronous and it runs in batches, don't use setState
inside a loop, rather create a new variable and set state at the end of the loop, like this setState
是异步的,它是分批运行的,不要在循环中使用setState
,而是创建一个新变量并在循环结束时设置 state ,就像这样
handleClickStart = () => {
let hours = this.state.hours;
while (hours > 0) {
hours --;
}
this.setState({hours});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.