2014-12-07 163 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

相關問題