简体   繁体   English

setTimeout不添加延迟

[英]setTimeout not adding a delay

This is my code: 这是我的代码:

function transition(index){
    $("#right-panel").find("img").each(function(){
        if ($(this).index() === index){
            setTimeout(function(){
                $(this).fadeIn(750);
            }, 100000000);
        }else{
            $(this).fadeOut(750);
        }
    });
}

For some reason, the setTimeout in the function is not causing a delay for the fadeIn. 由于某些原因,该函数中的setTimeout不会导致fadeIn延迟。 What am I doing wrong? 我究竟做错了什么?

this in the setTimeout callback isn't the same as outside it. thissetTimeout回调是不一样的外面。

var self = this;
setTimeout(function(){
    $(self).fadeIn(750);
}, 100000000);

Although you could just use .delay() . 尽管您可以只使用.delay()

$(this).delay(100000000).fadeIn(750)

Overall a better approach would seem to be to use .eq() to grab the one you want to .fadeIn() , and .fadeOut() the rest. 总体而言,一种更好的方法似乎是使用.eq()来捕获想要的.fadeIn() ,其余部分使用.fadeOut()

function transition(index){
    var images = $("#right-panel").find("img");// get all the images
    var fadein = images.eq(index)
                       .delay(100000000)
                       .fadeIn(750); // fadeIn the one at "index"
    images.not(fadein).fadeOut(750); // fadeOut all the others
}

Why do you need setTimeout at all? 为什么根本需要setTimeout?

function transition(index){
    $("#right-panel").find("img").each(function(){
        if ($(this).index() === index){ // did you check this statement?
            $(this).delay(100000000).fadeIn(750);
        }else{
            $(this).fadeOut(750);
        }
    });
}

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

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