Reputation: 117
how to stop time() when mouseout event occur?
`<div id="j" style="height:50px; width:50px; background-color:red;">Hello</div`>
$("#j").mouseenter(function(){
var count = 3;
var counterIncrement=1;
setInterval(timer, 1000);
function timer() {
count = count+counterIncrement;
if (count == 3 ) {
counterIncrement = +counterIncrement;
}
console.log(count);
}
});
i want to reset timer() function when mouseout even occur again mousein then start which count =3
Upvotes: 1
Views: 126
Reputation: 3819
You use the same interval in two DIFFERENT functions (event handles), so that you have to declare a global variable. At the very beginning of your script, declare:
var interval;
Assign the interval to this variable in one event handle:
interval = setInterval(.....);
and clear it when needed in another handle:
clearInterval(interval);
This would be it. Because the variable is globally declared, it will be recognized in both function scopes. It works, because the interval object is passed by reference.
Upvotes: 6
Reputation: 943450
setInterval()
clearInterval(the_value_you_returned_earlier)
Upvotes: 2