jQuery 设置超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8610272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery setTimeout
提问by user1002039
I'd like to add a timeout to this tooltip code so it only displays if the mouse hovers over it after a while rather than immediately... I tried adding the setTimeout()
but I could not figure out how to use the clearTimeout()
so the tooltip does not hide on mouseout. Can you help?
我想为这个工具提示代码添加一个超时,这样它只会在鼠标悬停在它上面一段时间而不是立即显示时显示......我尝试添加setTimeout()
但我不知道如何使用clearTimeout()
所以工具提示没有隐藏鼠标。你能帮我吗?
// -----------------------------------------------
// TOOLTIP MOUSE HOVER
// -----------------------------------------------
function mcTooltip() {
$('.mcTxb').mousemove(function(e) {
var mcHoverText = $(this).attr('alt');
var mcTooltip = $('.mcTooltip');
$(mcTooltip).text(mcHoverText).show('fast');
$(mcTooltip).css('top', e.clientY + 10).css('left', e.clientX + 10);
}).mouseout(function() {
var mcTooltip = $('.mcTooltip');
$(mcTooltip).hide('fast');
});
}
mcTooltip();
I tried this:
我试过这个:
// -----------------------------------------------
// TOOLTIP MOUSE HOVER
// -----------------------------------------------
function mcTooltip() {
$('.mcTxb').mousemove(function(e) {
var mcHoverText = $(this).attr('alt');
var mcTooltip = $('.mcTooltip');
setTimeOut(function(){
$(mcTooltip).text(mcHoverText).show('fast');
}, 300);
$(mcTooltip).css('top', e.clientY + 10).css('left', e.clientX + 10);
}).mouseout(function() {
var mcTooltip = $('.mcTooltip');
$(mcTooltip).hide('fast');
});
}
mcTooltip();
回答by Alnitak
As you're using animation, you can use .delay()
to defer the appearance of your tooltip:
当您使用动画时,您可以使用.delay()
来推迟工具提示的出现:
$(mcTooltip).text(mcHoverText).delay(1000).show('fast');
In your mouseout
function, use .stop
to cancel any existing delays or animations, and then hide the tooltip:
在您的mouseout
函数中,使用.stop
取消任何现有的延迟或动画,然后隐藏工具提示:
$(mcTooltip).stop(true).hide('fast');
回答by Jasper
var my_timer;
$('.mcTxb').hover(
function () {
my_timer = setTimeout(function () {
//do work here
}, 500);
},
function () {
clearTimeout(my_timer);
}
);
This will wait half a second before doing whateverwhen you mouseover
the element and the whateverwill not happen if you mouseout
within the half second.
这将做之前等待半秒,无论当你mouseover
的元素和任何你会不会发生mouseout
在半秒之内。
回答by Jasper
One option is to use the hoverIntentplugin to accomplish what you want.
一种选择是使用hoverIntent插件来完成你想要的。
回答by Purag
- Use
hover()
instead, it's less code (and that's always good, IMO).
- 改用
hover()
它,它的代码更少(这总是好的,IMO)。
Like so:
像这样:
function mcToolTip() {
$(".mcTxb").hover(function(){
// mouseover stuff here
$("element").delay(3000).show(); // 3 second delay, then show
}, function(){
// mouseout stuff here
});
}
回答by JSV
This question is old, but I thought I answer it for others. The main reason the timeout was not working was the case of "setTimeOut" . You cant camel hump the Out part. Its "setTimeout".
这个问题很老了,但我想我是替别人回答的。超时不起作用的主要原因是 "setTimeOut" 的情况。你不能驼峰输出部分。它的“setTimeout”。
回答by Anthony Carbon
setTimeoutis does not work on mouseover or hover. It is safe to use .delay().
setTimeout不适用于鼠标悬停或悬停。使用 .delay() 是安全的。
setTimeout(function(){
$(mcTooltip).text(mcHoverText).show('fast');
}, 300);
// use this instead.
$(mcTooltip).text(mcHoverText).delay(3000).show('fast');