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

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

    });

Answers


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

Example Look here


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.


i use new bind style of jQuery.

$(el).bind({
'mouseenter': function(){console.log('Mouse over');}, 
'mouseleave': function(){console.log('Mouse leave');}
});

I know this is kind of old, but I think the proper function is already in JavaScript, onmousemove does just that.


OBJ.addEventListener("mouseenter", function() {
  focus=true;
});
OBJ.addEventListener("mouseleave", function() {
  focus=false;
});

Just in case you don't want to use jquery you can use this :)


Need Your Help

Counting depth or the deepest level a nested list goes to

python list nested levels

A have a real problem (and a headache) with an assignment...

webpack: import + module.exports in the same module caused error

javascript import webpack ecmascript-6 require

I'm developing a website with webpack. When I have a code like this: