I am trying to use jQuery to program pong. I want the ball to be moving indefinitely but I don't know how to get my event to occur more than once. I am using keydown
and keyup
for moving the barrier at the bottom and want a way that moves the ball and allows me to continue moving the two barriers.
I have a function that determines which way to move the ball, and i want to repeat that function intermittently.
I tried moveball().repeat('Infinity');
and $(".ball").moveball().repeat('Infinity');
but I don't think this is the right syntax.
The relevant information from moveball()
is below:
function moveball(){
$(".ball").animate({top: '+='+ 40 + 'px'},10);
}
var loop = setInterval(moveball, 50);
this will repeat moveball every 50 milliseconds. call clearInterval(loop)
to stop it.