K. Daniek K. Daniek - 10 months ago 71
Javascript Question

Start a function when mouseover and stop it when mousemove

I want the function mozaic to start when mouseover and stop when mousemove. The first one works properly. Here's the code: http://codepen.io/Wisyr/pen/NRjyWj

$('#name').mouseover(function() {
var mozaic = setInterval(mosaic, 150);

$('#name').mouseout(function() {

function mosaic() {
setTimeout(function() {
}, 0);
setTimeout(function() {
}, 50);
setTimeout(function() {
}, 100);

Answer Source

try something like this.

You need the variable accessible in both functions so it needs to be declared outside those functions and then defined inside the first one and use clearInterval() to cancel an existing interval

var timer;
$('#name').mouseenter(function() {
   timer = setInterval(mosaic, 150);
}).mouseleave(function() {