An "if mouseover" or a "do while mouseover" in JavaScript/jQuery

Kyle picture Kyle · Oct 19, 2010 · Viewed 27k times · Source

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);

    });

Answer

jAndy picture jAndy · Oct 19, 2010
$('someObject').on('mouseenter', function() {
    this.iid = setInterval(function() {
       // do something           
    }, 25);
}).on('mouseleave', function(){
    this.iid && clearInterval(this.iid);
});

Example Look here