我必须调用一个由特定元素上的mouseenter
事件触发的函数。
只有当鼠标在特定元素上“站立”超过5秒时,我才能触发mouseenter
事件?
发布于 2015-07-28 19:40:48
您可以使用计时器(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
https://stackoverflow.com/questions/31675164
复制相似问题