【问题标题】:Trigger the event mouseenter only after specific time,仅在特定时间后触发事件 mouseenter,
【发布时间】:2015-07-28 11:35:58
【问题描述】:

我必须调用一个由特定元素上的mouseenter 事件触发的函数。

只有当鼠标在特定元素上“站立”超过 5 秒时,我才能触发 mouseenter 事件?

【问题讨论】:

标签: jquery jquery-events mouseenter


【解决方案1】:

您可以使用计时器(setTimeout()) 来安排一个函数在 5 秒后执行,如果鼠标在此之前离开,我们可以清除计时器,使其不会被调用。

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

【讨论】:

  • 我可以用 mouseenter 代替吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多