简体   繁体   English

jQuery函数创建异常暂停

[英]Jquery function creates an anomolous pause

EDIT: created a fiddle which shows the problem https://jsfiddle.net/MichaelCaley/wcoba1jy/ 编辑:创建了一个小提琴,显示了问题https://jsfiddle.net/MichaelCaley/wcoba1jy/

The below code creates a 7 second pause between each fade until we get to the end where the code re runs itself. 下面的代码在每次淡入之间创建7秒钟的暂停,直到我们结束代码重新运行为止。 For some reason this delay becomes 14/15 seconds. 由于某种原因,此延迟变为14/15秒。

I have done a lot of searching but come up with trump all, so any help is appreciated. 我做了很多搜索,但都拿出了王牌,因此对您的帮助表示感谢。

    var imgshow = function(){
    $(".img1").delay(7000).fadeOut(1500, function(){
        $(".img2").delay(7000).fadeOut(1500, function(){
            $(".img1").delay(7000).fadeIn(1500, function(){
                $(".img2").delay(7000).fadeIn(1500, function(){
                    imgshow();
                });
            });
        });
    });
}
imgshow();

It's actually functioning as coded (but probably not as you intended :-)). 它实际上是按编码运行的(但可能不是您想要的:-))。 What's happening is that the 2nd fadeIn has (visually) no effect because img2 is under img1 (which is fully opaque) causing img1 to stay twice as long. 发生的是,第二个fadeIn (在视觉上)没有任何作用,因为img2在img1下(完全不透明),导致img1停留了两倍的时间。

Hopefully the following makes it clear - 1 indicates fully opaque and 0 indicates fully transparent 希望以下内容可以清楚地说明-1表示完全不透明,0表示完全透明

  • img3 (1) under img2 (1) under img1 (1) => img1 shown img1(1)下img2(1)下的img3(1)=>显示的img1
  • img3 (1) under img2 (1) under img1 (0) => img2 shown img1(0)下img2(1)下的img3(1)=>显示的img2
  • img3 (1) under img2 (0) under img1 (0) => img3 shown img1(0)下img2(0)下的img3(1)=>显示的img3
  • img3 (1) under img2 (0) under img1 (1) => img1 shown img1(1)下img2(0)下的img3(1)=>显示的img1
  • img3 (1) under img2 (1) under img1 (1) => img1 shown (when you probably want img2) ... img3(1)在img2(1)在img1(1)下=>显示img1(当您可能需要img2时)...

If you swap the last 2 fadeIn , you'll get something that changes color every interval ( https://jsfiddle.net/xx303tdc/ ) but the sequence will be img1, img2, img3, img2, img1... Otherwise you could just set img2 to be fully visible after the img1 fadeIn and then restart the loop. 如果交换最后2个fadeIn ,则会得到一些在每个时间间隔都会更改颜色的内容( https://jsfiddle.net/xx303tdc/ ),但顺序为img1,img2,img3,img2,img1 ...否则,您可以只需将img2设置为在img1 fadeIn之后完全可见,然后重新启动循环即可。

In any case, because you are doing a recursive call, you'll eventually run out of stackspace (very slowly because of the delay), so it might be a good idea to refactor to use CSS transitions. 无论如何,由于您正在进行递归调用,最终将耗尽堆栈空间(由于延迟,速度非常缓慢),因此重构使用CSS过渡可能是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM