Reputation: 364
I am making a weird block movement thing, but after it moved 10 times it says:
Uncaught RangeError: Maximum call stack size exceeded
And my purpose is to let it move the whole time, here is the code 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>
Upvotes: 0
Views: 160
Reputation: 40872
Your problem is that:
$(square).animate({marginTop:randomtop, marginLeft:randomleft}, duration, movesquare);
Will call movesquare
immediately when duration
is 0
or smaller.
At the time this happens you created an endless loop.
You need to make sure that duration
does not become 0
.
Upvotes: 3