我正在制作一个奇怪的 block 移动东西,但在它移动 10 次后它说:
Uncaught RangeError: Maximum call stack size exceeded
我的目的是让它一直移动,顺便说一句,这是代码:
<html>
<head>
<title>Look - The game</title>
</head>
<body>
<div id="square" style="position:absolute; width:5px; height:5px; background:black"></div>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
var square = document.getElementById("square");
var duration = 1000;
var steps = 1;
function movesquare(){
var randomtop = Math.floor(Math.random() * screen.height);
var randomleft = Math.floor(Math.random() * screen.width);
$(square).animate({marginTop:randomtop, marginLeft:randomleft}, duration, movesquare);
duration -= steps;
steps = steps * 2;
}
movesquare();
</script>
</body>
最佳答案
你的问题是:
$(square).animate({marginTop:randomtop, marginLeft:randomleft}, duration, movesquare);
当duration
为0
或更小时,将立即调用movesquare
。
发生这种情况时,您创建了一个无限循环。
您需要确保duration
不会变成0
。
关于javascript - 是否可以删除最大调用堆栈大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27344385/