【发布时间】:2015-07-28 11:35:58
【问题描述】:
我必须调用一个由特定元素上的mouseenter 事件触发的函数。
只有当鼠标在特定元素上“站立”超过 5 秒时,我才能触发 mouseenter 事件?
【问题讨论】:
-
不同事件相同问题/解决方案:jQuery .keyup() delay
标签: jquery jquery-events mouseenter
我必须调用一个由特定元素上的mouseenter 事件触发的函数。
只有当鼠标在特定元素上“站立”超过 5 秒时,我才能触发 mouseenter 事件?
【问题讨论】:
标签: jquery jquery-events mouseenter
您可以使用计时器(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
【讨论】: