views:

78

answers:

2

Hi there,

I am looking for a way to repeat a mouseover action until the user moves away from the target. A mouseover invokes a function once, I am looking for a way to keep doing the function.

Cheers, Gazler.

+4  A: 

You'll need to use setInterval():

var to;
var doStuff = function() {
    console.log('doing stuff...');
};

$('a').hover(function(e) {
    to = window.setInterval(doStuff, 1);
},function(e) {
    window.clearInterval(to);
})
David
Perfect, cheers.
Gazler
You are using a single timer variable for all anchor tags - this works most of the time, however it might not work as expected when you have nested anchor tags - due to event bubbling you will create TWO (or even more) intervals - one for each nested element. you will however only clear ONE
dionadar
@dionadar: Nested anchors are illegal: http://www.w3.org/TR/html401/struct/links.html#h-12.2.2
David
A: 
//continuous


var timer;

var doStuff=function(quit){

  console.log('doing stuff');

  if (quit!==true){

    timer=setTimeout(doStuff, 100);

  }

  else{

    clearTimeout(timer);

  }

};

$('div#continuous').bind('mouseenter', doStuff).bind('mouseleave', function(){doStuff(true);});
czarchaic