I use the following script to move elements in a canvas randomly:
function makeNewPosition(){
var h = $(window).height() - 50;
var w = $(window).width() - 50;
var nh = Math.floor(Math.random() * h);
var nw = Math.floor(Math.random() * w);
return [nh,nw];
}
function animateDiv(element){
var newq = makeNewPosition();
$(element).animate({ top: newq[0], left: newq[1] }, 5000, function(){
animateDiv(element);
});
};
Now i need to immediately stop the animation when clicking a button. How is this possible?