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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:26:55  来源:igfitidea点击:

Callback to .delay()

jquery

提问by Randomblue

I have an $imagethat I .fadeInand .fadeOut, and then .removeafter .fadeOutcompletes. This is my code:

我有一个$imageI.fadeIn.fadeOut,然后.remove.fadeOut完成之后。这是我的代码:

$image
   .fadeIn()
   .fadeOut(function() {
      $(this).remove();
   });

I want to add a .delayafter .fadeOut, and .removethe $imageonly once .delayhas completed. I have tried:

我想添加.delay.fadeOut,并.remove$image一次.delay完成。我试过了:

$image
   .fadeIn()
   .fadeOut()
   .delay(1000, function() {
      $(this).remove();
   });

The problem is that .delaydoest notaccept a callback function. How can I .removethe picture as a callback to .delay?

问题是,.delay所行接受一个回调函数。如何.remove将图片作为回调到.delay

回答by Frédéric Hamidi

You can use the queue()method to schedule your own function to run after delay()completes:

您可以使用queue()方法来安排您自己的函数在delay()完成后运行:

$image.fadeIn()
      .fadeOut()
      .delay(1000)
      .queue(function(next) {
          $(this).remove();
          next();
      });

回答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 fxqueue you might need to specify one.

阅读文档以获取有关您延迟哪个队列的更多信息,如果您在使用默认fx队列时遇到问题,您可能需要指定一个。