Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fire event after 3 sec of hovering

I have a div and I want to fire an event only after user continuous hovers his mouse for 3 sec. My code doesn't work well because it fires right after hover and doesn't "wait".

Code:

$(".inner_pic").mouseenter(function () {
    setTimeout(function () {
        alert('testing');
    }, 3000);
}).mouseleave(function () {
    alert('finish');
});  
like image 746
sortof Avatar asked Jun 30 '15 13:06

sortof


6 Answers

You need to store timeout id somewhere and clear it on mouseout. It's convenient to use data property to save this id:

$(".inner_pic").mouseenter(function () {
    $(this).data('timeout', setTimeout(function () {
        alert('testing');
    }, 3000));
}).mouseleave(function () {
    clearTimeout($(this).data('timeout'));
    alert('finish');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="inner_pic">PICTURE</div>
like image 72
dfsq Avatar answered Sep 30 '22 03:09

dfsq


You can achieve this by delay option:

Working demo

$('#elem').popover({
    trigger: "hover",
    delay: {show : 3000, hide : 0} });
like image 21
yakutsa Avatar answered Sep 30 '22 02:09

yakutsa


Checkout the below code

var myVar;
$( "div#container" )
  .mouseover(function() {
    myVar = setTimeout(function(){ alert("Hello"); }, 3000);
  })
  .mouseout(function() {
    clearTimeout(myVar);
  });
div {
  background: red;
  margin: 20px;
  height: 100px;
  width: 100px;
  display:block;
  cursor: pointer;
  }
div:hover {
  background: yellow;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container"></div>
like image 30
Pratik Shah Avatar answered Sep 30 '22 01:09

Pratik Shah


    
    var st;
    $(".inner_pic").mouseenter(function(e) { 
        var that = this;
        st = setTimeout(function() {
            alert('testing');
        }, 3000);
    }).mouseleave(function() {
         clearTimeout( st );    
         alert('finish'); 
    });  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="inner_pic">
  <h3>Picture Here - Hover me</h3>
</div>
like image 24
PeterKA Avatar answered Sep 30 '22 03:09

PeterKA


Assuming you have a div with id of myelement, you can do this:

var divMouseOver;
$('#myelement').mouseover(function() {
  divMouseOver = setTimeout(function() {
     alert("3 seconds!"); //change this to your action
  }, 3000);
});
$('#myelement').mouseout(function() {
  if (divMouseOver) {
    clearTimeout(divMouseOver);
  }
});

BTW, tere's a helpful clarifying question re: using mouseenter and mouseover right here: Jquery mouseenter() vs mouseover(). Consider this when choosing which to use.

like image 31
Marc Avatar answered Sep 30 '22 01:09

Marc


Try This Code:

<div id='a' style="border:2px solid black" >
    <h3>Hove On me</h3>
    For 2000 milliseconds. You will get an alert.
    </br>
    </div>

$(document).ready(function(){
   var delay=2000, setTimeoutConst;
   $('#a').hover(function(){
        setTimeoutConst = setTimeout(function(){
            /* Do Some Stuff*/
           alert('Thank You!');
        }, delay);
   },function(){
        clearTimeout(setTimeoutConst );
   })
})

</script>

DEMO:

http://jsfiddle.net/uhwzzu1u/1/

like image 34
PHP Worm... Avatar answered Sep 30 '22 01:09

PHP Worm...