尝试创建一个可以停止使用函数 doIt() 的无限 for 循环
大家好,我是 javascript 新手,我正在为我的期末考试做一个项目。我需要做到这一点,以便我操纵的这个游戏引擎导致生成按钮进入无限循环。
我还需要使用 (Reset==1) 来停止它。有什么帮助吗?这是我到目前为止的代码(如果有帮助的话):
function generation()
{
for(y2=0; y2<2500; y2++)
{
tempmapactual[y2]=mapactual[y2];
}
for (g=0;g<2500;g++)
{
neighbours=0;
for (h=0;h<8;h++)
{
if (g+coords[h]>0 && g+coords[h]<2499 && mapactual[g+coords[h]]=="white.gif")
{neighbours=neighbours+1;}
}
if (neighbours>=4 || neighbours==1 || neighbours==0)
{tempmapactual[g]="black.gif";}
if (neighbours==3) {tempmapactual[g]="white.gif";}
}
for(y3=0; y3<2500; ++y3)
{
if (mapactual[y3]!=tempmapactual[y3])
{
mapactual[y3]=tempmapactual[y3];
document.images[y3+offset].src=mapactual[y3];
}
}
}
</script>
<script>
function doIt()
{
for (i=0; i<X; i++)
{
// This is where I have trouble. What part of generation() do I call?
}
if (Reset==1) break; // This will kill the loop instantly.
}
}
</script>
<script>
window.onload(doIt($(X).value)));
</script>
<form>
<input type="button" value="generate" onClick="generation();">
</form>
<form>
<input type="text">
</form>
<form>
<input type="button" value="Infinite Loop!" onclick="doIt();">
</form>
<form>
<input type="button" value="Reset" onclick="doIt();">
</form>
Hey guys, I'm new to javascript and I'm doing a project for my final in class. I need to make it so this game engine I manipulated causes the generation button to go for an infinite loop.
I also need to stop it using (Reset==1). Any help? Here's the code I have so far if that helps:
function generation()
{
for(y2=0; y2<2500; y2++)
{
tempmapactual[y2]=mapactual[y2];
}
for (g=0;g<2500;g++)
{
neighbours=0;
for (h=0;h<8;h++)
{
if (g+coords[h]>0 && g+coords[h]<2499 && mapactual[g+coords[h]]=="white.gif")
{neighbours=neighbours+1;}
}
if (neighbours>=4 || neighbours==1 || neighbours==0)
{tempmapactual[g]="black.gif";}
if (neighbours==3) {tempmapactual[g]="white.gif";}
}
for(y3=0; y3<2500; ++y3)
{
if (mapactual[y3]!=tempmapactual[y3])
{
mapactual[y3]=tempmapactual[y3];
document.images[y3+offset].src=mapactual[y3];
}
}
}
</script>
<script>
function doIt()
{
for (i=0; i<X; i++)
{
// This is where I have trouble. What part of generation() do I call?
}
if (Reset==1) break; // This will kill the loop instantly.
}
}
</script>
<script>
window.onload(doIt($(X).value)));
</script>
<form>
<input type="button" value="generate" onClick="generation();">
</form>
<form>
<input type="text">
</form>
<form>
<input type="button" value="Infinite Loop!" onclick="doIt();">
</form>
<form>
<input type="button" value="Reset" onclick="doIt();">
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果没有线程,您无法从另一个函数中跳出已经运行的无限循环;但您可以采取稍微不同的方法:如果您的“无限循环”是通过运行一次迭代的函数发生的,然后使用 setTimeout 回调在一段时间后再次调用自身,那么它将运行“连续地”,同时仍然是可中断的(例如,在 doIt 函数中设置一些全局标志变量,
Generation 可以检查它何时被调用,如果为真则退出。
You can't break out of an already-running infinite loop from another function without threads; but you could take a slightly different approach: if your "infinite loop" happens by way of the function running one iteration and then using a
setTimeout
callback to call itself again after an interval, then it'll run 'continuously', while still being interruptable (by, say, setting some global flag variable in yourdoIt
function, whichgeneration
can check when it's invoked, and exit if it's true.我想你想看看这篇关于定时延迟的文章。每当 JavaScript 忙于计算(包括使用
generate()
)时,它就会对按钮按下等无响应。你想限制这种情况发生的次数。I think you want to look at this article about timing delays. Whenever JavaScript is busy going calculates (including with
generate()
) it is unresponsive to button pushing and such. You want to limit how much that happens.JavaScript 不是多线程的,你无法轻易摆脱无限循环。大多数浏览器/JavaScript 会检测到失控的旋转并终止执行。
如果您正在编写游戏引擎,更好地利用 CPU 周期的方法是设置计时器。这样您就可以在不再需要时停止计时器。
JavaScript is not multithreaded and you cannot break out easily of an infinite loop. Most browsers/JavaScript will detect out of control spinning and kill execution.
If you are writing a game engine, a better use of your CPU cycles is to set a timer. That way you can stop the timer when it is no longer needed.