简体   繁体   English

当我运行我的程序时,我正在摆脱 memory 错误

[英]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.

相关问题 运行此程序时出现未经授权的 401 错误 - getting unauthorized 401 error when I'm running this 我的测试有问题,当我运行它们时出现错误 - I'm having a problem with my test, when I run them I'm getting error 使用节点运行编译的 JavaScript 代码时出错 - Error when i'm running my compiled JavaScript code with node 操作数组时出现错误 - I'm getting error when manipulating array 我正在运行程序时内存不足。 有没有更好的方法做到这一点,或者我是否创建了无限循环? - I am getting running out of memory when I am running my program. is there a better way to do it or am I creating any infinite loops? 为什么当我尝试运行服务器 gRPC 时出现此错误? - why i'm i getting this error when i try to run my server gRPC? 当我试图启动我的反应应用程序时,我收到一个不寻常的错误。 它说CleanWebpackPlugin不是构造函数 - I'm getting an unusual error when I'm trying to start my react app. It says that CleanWebpackPlugin is not a constructor 在本地主机中运行应用程序时出现错误.XMLHttpRequest无法加载 - I'm getting error while running application in my local host.XMLHttpRequest cannot load 我正在运行 'yarn build' 来完成 salesforce 插件,我收到了一些错误消息 - I'm running 'yarn build' to finalize the salesforce plugin and I'm getting some error messages 我收到了NaN错误 - I'm getting a NaN error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM