ofey
ofey

Reputation: 3347

Repeatedly run function on mouseover

Trying to get a function to run continuously when the mouse is over a div. How can I get the repeat() function to only run when the mouse is over the div?

function repeat(){
  window.setInterval(console.log("Mouse is on image"), 1000)};
$("div.mainGif").mouseover(repeat());

I also tried it this way using an anonymous function,

$("div.mainGif").mouseover(function(){window.setInterval(console.log("Mouse is on image"), 1000)});

But that doesn't work at all.

Upvotes: 0

Views: 33

Answers (1)

connexo
connexo

Reputation: 56773

Check this and give us a note how it goes:

var interval;
function repeat(){
  interval = window.setInterval(function() { console.log("Mouse is on image") }, 1000)
};
$("div.mainGif").on({
  mouseover: repeat,
  mouseout: function() { window.clearInterval(interval); }
});

Upvotes: 2

Related Questions