admin管理员组文章数量:1279090
Yes, I know this question has been asked before, but I can't find an answer that works. This is an accepted answer from one of the other questions:
$('#element').hover(function()
{
$(this).data('timeout', window.setTimeout(function()
{
alert('hovered for 2 seconds');
}, 2000));
},
function()
{
clearTimeout($(this).data('timeout'));
alert('mouse left');
});
/
As you see it doesn't do what it's supposed to.
What I need is simple in theory but I can't get it to work - when a user hovers over a link for 2 seconds, a function is called. If the user moves the mouse away before 2 seconds pass, nothing happens.
Yes, I know this question has been asked before, but I can't find an answer that works. This is an accepted answer from one of the other questions:
$('#element').hover(function()
{
$(this).data('timeout', window.setTimeout(function()
{
alert('hovered for 2 seconds');
}, 2000));
},
function()
{
clearTimeout($(this).data('timeout'));
alert('mouse left');
});
http://jsfiddle/nCcxt/
As you see it doesn't do what it's supposed to.
What I need is simple in theory but I can't get it to work - when a user hovers over a link for 2 seconds, a function is called. If the user moves the mouse away before 2 seconds pass, nothing happens.
Share Improve this question asked Jun 16, 2012 at 11:34 sveti petarsveti petar 3,79714 gold badges77 silver badges158 bronze badges1 Answer
Reset to default 14The code works perfectly fine. It only breaks due to the alert()
calls which causes the mouseout event to be triggered.
What do we learn from it? Do not use alert()
in bination with focus/hover/mousemove-related events.
By the way, there are already jQuery plugins available for what you want to do: http://cherne/brian/resources/jquery.hoverIntent.html
本文标签: javascriptMake function execute after user hovers over link for 2 secondsStack Overflow
版权声明:本文标题:javascript - Make function execute after user hovers over link for 2 seconds - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741274618a2369656.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论