Javascript 是否可以删除最大调用堆栈大小?
我正在做一个奇怪的方块运动,但当它移动10次后,它说:Javascript 是否可以删除最大调用堆栈大小?,javascript,jquery,Javascript,Jquery,我正在做一个奇怪的方块运动,但当它移动10次后,它说: Uncaught RangeError: Maximum call stack size exceeded 我的目的是让它一直移动,下面是代码: <html> <head> <title>Look - The game</title> </head> <body> <div id="square" styl
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>
看-游戏
var square=document.getElementById(“square”);
var持续时间=1000;
var步长=1;
函数movesquare(){
var randomtop=Math.floor(Math.random()*screen.height);
var randomleet=Math.floor(Math.random()*screen.width);
$(square).animate({marginTop:randomtop,marginLeft:randomleft},duration,movesquare);
持续时间-=步数;
步骤=步骤*2;
}
movesquare();
您的问题是:
$(square).animate({marginTop:randomtop, marginLeft:randomleft}, duration, movesquare);
当duration
为0
或更小时,将立即调用movesquare
。
在这种情况发生时,您创建了一个无止境的循环
您需要确保duration
不会变成0
您的问题是:
$(square).animate({marginTop:randomtop, marginLeft:randomleft}, duration, movesquare);
当duration
为0
或更小时,将立即调用movesquare
。
在这种情况发生时,您创建了一个无止境的循环
您需要确保duration
不会变成0
您的问题是:
$(square).animate({marginTop:randomtop, marginLeft:randomleft}, duration, movesquare);
当duration
为0
或更小时,将立即调用movesquare
。
在这种情况发生时,您创建了一个无止境的循环
您需要确保duration
不会变成0
您的问题是:
$(square).animate({marginTop:randomtop, marginLeft:randomleft}, duration, movesquare);
当duration
为0
或更小时,将立即调用movesquare
。
在这种情况发生时,您创建了一个无止境的循环
您需要确保
duration
不会变为0
使用递归有什么原因吗?也许这是无意的?这是导致堆栈问题的原因。您正在做错事<代码>超过最大调用堆栈大小表示存在一种无止境的循环,如果浏览器不停止脚本,则在这种情况下,浏览器很可能会变得无响应。我想让它一直移动,或者有其他方法可以做到这一点。顺便说一句,@LeoDeng只有在duration
小于或等于零时才是递归的。只要它大于零,就不会有递归。使用递归有什么原因吗?也许这是无意的?这是导致堆栈问题的原因。您正在做错事<代码>超过最大调用堆栈大小表示存在一种无止境的循环,如果浏览器不停止脚本,则在这种情况下,浏览器很可能会变得无响应。我想让它一直移动,或者有其他方法可以做到这一点。顺便说一句,@LeoDeng只有在duration
小于或等于零时才是递归的。只要它大于零,就不会有递归。使用递归有什么原因吗?也许这是无意的?这是导致堆栈问题的原因。您正在做错事<代码>超过最大调用堆栈大小表示存在一种无止境的循环,如果浏览器不停止脚本,则在这种情况下,浏览器很可能会变得无响应。我想让它一直移动,或者有其他方法可以做到这一点。顺便说一句,@LeoDeng只有在duration
小于或等于零时才是递归的。只要它大于零,就不会有递归。使用递归有什么原因吗?也许这是无意的?这是导致堆栈问题的原因。您正在做错事<代码>超过最大调用堆栈大小表示存在一种无止境的循环,如果浏览器不停止脚本,则在这种情况下,浏览器很可能会变得无响应。我想让它一直移动,或者有其他方法可以做到这一点。顺便说一句,@LeoDeng只有在duration
小于或等于零时才是递归的。只要它大于零,就不会有递归。谢谢,我没有意识到持续时间将在10个步骤中变为小于0。谢谢,我没有意识到持续时间将在10个步骤中变为小于0。谢谢,我没有意识到持续时间将在10个步骤中变为小于0。谢谢,我没有意识到持续时间将在10步中变为小于0。