我的代码可以工作,并且所有值都正确,因此应该可以运行,但不能。我尝试过对变量进行本地化,更改时间以及重新排列功能和名称标签。auto1(); var autocount = 0;var autotrue = 0;function auto1(){ setTimeout(function() { while(autotrue==1){ money = money + autocount; setText("money_display",money); } }, 1000); onEvent("auto1", "click", function(){ if(money >= 10){autotrue = 1; money = money - 10; autocount = autocount+1; console.log("You now have " + autocount + " J$ per second"); } else { console.log("you have insufficient J$ for this purchase"); } });}我希望它每1000毫秒为我的money变量加1。但这对货币变量无济于事
3 回答

胡子哥哥
TA贡献1825条经验 获得超6个赞
这里有一些问题:
setTimeout
仅在1000毫秒结束时运行一次。在这种情况下,当它运行时,您将进入一个无限循环,因为autotrue永远不会设置为true。它仍然是0,并且您将其添加到金钱中,金钱将永远不会超过10,因为0 + 0 = 0。
如果您想每1000毫秒重复赚钱,则可以使用setInterval
,而不会出现任何循环。每隔1000毫秒就会调用一次函数。

达令说
TA贡献1821条经验 获得超6个赞
你叫auto1
变量之前autocount
和autotrue
被初始化为0,因此他们仍然是undefined
和将打破你的计算。您应该在初始化所有变量之后调用该函数。
另外while(autotrue==1){
,它看起来像是无限的,因为没有任何变化autotrue
。无限循环总是不好的。
添加回答
举报
0/150
提交
取消