views:

63

answers:

2

Is there a JavaScript or jQuery solution to run a function repeatedly (after setTimeout) while the mouse is over a DOM object? Otherwise said, is there a JavaScript "do while mouseover" (or "if mouseover")?

    $('someObject').bind('mouseover', function() {

        //Do the following while mouseover 
        $('someOtherObject').css('margin-left',adjustedLeft + 'px');
        setTimeout(/*do it again*/,25);

    });
A: 

I would solve this issue using the onmouseout event. Start whatever you intended to do while the mouse is over the specified component on the mouseover event. When onmouseout event occurs i would stop it.

Thariama
+9  A: 
$('someObject').bind('mouseenter', function() {
    this.iid = setInterval(function() {
       // do something           
    }, 25);
}).bind('mouseleave', function(){
    this.iid && clearInterval(this.iid);
});

Example: http://www.jsfiddle.net/YjC6y/29/

jAndy
+1, was trying to write an explanation for this ..
Gaby
+1 Clear and simple.
jensgram
Oh that's cool!! Great example too!
Emile
+1 great example.
Steve Claridge