1

I have to call a function which is trigger by mouseenter event on specific element.

How can I trigger the mouseenter event only if the mouse "stands" more than 5 sec on the specific element?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sarit Rotshild
  • 391
  • 3
  • 5
  • 21
  • Different event same problem/solution: [jQuery .keyup() delay](http://stackoverflow.com/questions/1909441/jquery-keyup-delay) – Andreas Jul 28 '15 at 11:39
  • Maybe this helps: http://stackoverflow.com/questions/2060601/javascript-cant-stop-the-settimeout – connexo Jul 28 '15 at 11:44

1 Answers1

3

You can use a timer(setTimeout()) to schedule a function after 5 seconds, if the mouse leaves before that then we can clear the timer so that it won't be invoked.

var timer;
$('#me').hover(function() {
  timer = setTimeout(function() {
    snippet.log('called after 5000')
  }, 5000);
}, function() {
  //if leaves early then clear the timer
  clearTimeout(timer);
})
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Some text
<div id="me">hover me for more time</div>
more content
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531