I'm not sure how to ask the question rattling around in my head right now, so bear with me. I'm brand new to asynchronous programming, and I figured the best way to learn would be to make a little javascript pong game. I started with a shootball() function and just bounce a div around another div. How I did this was with something like this:
function shootball(angle, speed){
angle = (angle/360.0)*2*Math.PI;
var ballmotion = setInterval(function(){
var nowx, nowy, minusY, plusX;
nowx = $("#ball").position().left;
nowy = $("#ball").position().top;
minusY = Math.sin(angle) * 4.0;
plusX = Math.cos(angle) * 4.0;
if(hitsWall(nowx+plusX, nowy-minusY)){
clearInterval(ballMotion);
shootball(newAngle(nowx+plusX, nowy-minusY), speed);
}
$("#ball").css("left", (nowx + plusX)).css("top", (nowy - minusY));
}, 10/speed);
}
I'm not a big fan of big unnecessary recursion, but I just wanted to try it out. Lo and behold it works exactly as I would expect. But as I started fleshing out the rest of the program, it occurred to me that I had no way of avoiding this recursive nature. So my question: Does javascript somehow recognize that the calling "shootball" function is essentially finished after calling clearInterval? Or does this really find itself loading up my stack with unnecessary activation records? Thanks in advance for any expertise this may drum up.