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:

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

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

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

    setTimeout(function() {
    }, 50);

    setTimeout(function() {
    }, 100);
By : K. Daniek


As stated by Kevin B, the mouse will move when over the text and prevent the animation from being visible.

But to achieve what you have asked for, use the following

var mozaic;

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

$(document).mousemove(function() {

Note, that the mosaic variable has to be global, so that the mousemove function has access to it. Initializing it inside the mouseover function will limit its scope to within that function.

Maybe to achieve what you want (effect wise) try to use mouseout on the text.

$('#name').mouseout(function() {
By : JHolub

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() {

This video can help you solving your question :)
By: admin