jQuery 回调到 .delay()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7915140/
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
Callback to .delay()
提问by Randomblue
I have an $image
that I .fadeIn
and .fadeOut
, and then .remove
after .fadeOut
completes. This is my code:
我有一个$image
I.fadeIn
和.fadeOut
,然后.remove
在.fadeOut
完成之后。这是我的代码:
$image
.fadeIn()
.fadeOut(function() {
$(this).remove();
});
I want to add a .delay
after .fadeOut
, and .remove
the $image
only once .delay
has completed. I have tried:
我想添加.delay
后.fadeOut
,并.remove
在$image
一次.delay
完成。我试过了:
$image
.fadeIn()
.fadeOut()
.delay(1000, function() {
$(this).remove();
});
The problem is that .delay
doest notaccept a callback function. How can I .remove
the picture as a callback to .delay
?
问题是,.delay
所行不接受一个回调函数。如何.remove
将图片作为回调到.delay
?
回答by Frédéric Hamidi
回答by Lapple
You can always do it as:
你总是可以这样做:
$image
.fadeIn()
.fadeOut(function() {
var self = this; // Not sure if setTimeout
// saves the pointer to this
setTimeout(function() {
$(self).remove();
}, 1000)
});
回答by Grant Thomas
To my knowledge, you can just strap the calls on afterthe delay call, like this:
据我所知,您可以在延迟调用之后继续调用,如下所示:
$image
.fadeIn()
.fadeOut()
.delay(1000)
.remove()
});
Such as in the following example from the documentation:
例如在文档中的以下示例中:
$('#foo').slideUp(300).delay(800).fadeIn(400);
The temperament of queued items execution spelled out there also:
排队项目执行的气质也说明了:
...the .delay() method allows us to delay the execution of functions that follow it in the queue. It can be used with the standard effects queue or with a custom queue. Only subsequent events in a queue are delayed; for example this will not delay the no-arguments forms of .show() or .hide() which do not use the effects queue.
....delay() 方法允许我们延迟队列中跟随它的函数的执行。它可以与标准效果队列或自定义队列一起使用。只有队列中的后续事件被延迟;例如,这不会延迟不使用效果队列的 .show() 或 .hide() 的无参数形式。
Read the documentation for further information regarding whichqueue you're delaying, if you have troubles with the default fx
queue you might need to specify one.
阅读文档以获取有关您延迟哪个队列的更多信息,如果您在使用默认fx
队列时遇到问题,您可能需要指定一个。