2014-12-07 151 views
0

我想提出一个奇怪的块体运动的事情,但在它移动的10倍,它说:是否可以删除最大调用堆栈大小?

Uncaught RangeError: Maximum call stack size exceeded 

而我的目的是要让它动的整个时间,这里是代码BTW:

<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> 

+0

你有使用递归的原因吗?也许这是无意的?这是你的堆栈问题的原因。 – 2014-12-07 15:49:51

+0

你正在做错什么...! – GOD 2014-12-07 15:50:14

+3

'超出最大调用堆栈大小'表示您有一种_endless loop_,并且如果浏览器不会停止您的脚本,则在这种情况下,您的浏览器很可能会变得无响应。 – 2014-12-07 15:50:32

回答

3

您的问题是:

$(square).animate({marginTop:randomtop, marginLeft:randomleft}, duration, movesquare); 

下,CA当duration0或更小时,立即使用movesquare。 在发生这种情况时,您创建了一个无限循环

您需要确保duration不会成为0

+0

谢谢,我没有意识到持续时间会在10个步骤中变得低于0。 – Ebbez 2014-12-07 15:58:36

相关问题