I managed to make a simple loop to move my character, onmousedown I move the character diagonally to where the user clicked. It's working but my problem is that I don't know how to prevent more loops to start over the first if the user clicks more than once.
Right now if you click more than once the character moves faster and faster since more loops are running at the same time.
This is the loop:
(function move() {
if(cframes<frames) {
cframes = cframes+1;
character.style.left=parseInt(character.style.left)+pxsecx;
character.style.top=parseInt(character.style.top)+pxsecy;
setTimeout(move, 20);
}
})()
}
How do I prevent this loop from starting if it already has? or stop it and start another? Thanks
(function)()
pattern, it can be handy for library stuff, but you don't need it, and you probably don't have the understanding to gain any advantage by using it. \$\endgroup\$