我決定搞亂jquery,製作一個簡單的基於軸測圖的遊戲。它呈現一些地圖,然後在堆棧中出現問題。 (超過最大調用堆棧大小) 我現在只是使用背景顏色作爲瓷磚。爲什麼這個循環會導致堆棧溢出?
var map = [
"g","g","g","s","s",
"g","g","s","s","w",
"g","g","s","w","w",
"g","g","s","w","w",
"g","g","s","w","w"
];
var x = 0;
var y = 0;
var count = 0;
var background;
function init() { $.each(map, function(key, value) {
x += 30;
y += 60;
if (count > 4){
x -= 120;
y -= 360;
count = 0;
}
if (value == "g"){background = "#00ff00";}
if (value == "w"){background = "#0000ff";}
if (value == "s"){background = "#ffff00";}
$('.map_piece').clone().appendTo('body').css({'top' : x + 'px', 'left' : y + 'px','display' : 'block','background' : background});
count ++;
});
}
$('document').ready(function(){
init();
});
非常感謝您的糾正和原因。我對克隆不太熟悉,我沒有意識到我克隆了每一個實例。這兩個答案都很有幫助,但是由於你先回答2分鐘......你會得到複選標記! :) – 2014-09-28 04:49:55