i am working on this random stuff here. like a random Div hovering around. Now when mouse is hovered the function starts but how can I automatically stop the function called animateDiv() after some time. thanks. here is the code.
html code
<div class="a">
<button id="myButton">Click Me ;)</button>
</div>
and jquery code
$(document).ready(function () {
$('.a').mouseenter(function() {
animateDiv();
});
});
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() {
var newq = makeNewPosition();
$('.a').animate(
{top: newq[0],
left: newq[1], }
,400, function () {
animateDiv();
});
};
setTimeoutandclearTimeoutinstead.