的setTimeout不加延迟
这是我的代码:的setTimeout不加延迟
function transition(index){
$("#right-panel").find("img").each(function(){
if ($(this).index() === index){
setTimeout(function(){
$(this).fadeIn(750);
}, 100000000);
}else{
$(this).fadeOut(750);
}
});
}
出于某种原因,在功能上的setTimeout不会导致对淡入延迟。我究竟做错了什么?
在setTimeout
回调this
是不一样的外面。
var self = this;
setTimeout(function(){
$(self).fadeIn(750);
}, 100000000);
虽然你可以只使用.delay()
。
$(this).delay(100000000).fadeIn(750)
总体更好的方法似乎是使用.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
}
ooooooh现在我明白了 – 2012-01-27 22:22:01
为什么你需要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);
}
});
}
谢谢,这是诀窍:) – 2012-01-27 22:20:26
只是一个音符 - 延迟使用setTimeout ..使用延迟的一个好处是不要担心这个参考。 – 2012-01-27 22:25:03
@SKS我的意思是专门为他的例子。 – Cheery 2012-01-27 22:26:16
我宁可怀疑fadeIn没有发生 - 请确认吗? – Alnitak 2012-01-27 22:17:35
为什么延迟28小时?你尝试过一个较小的(合理的)价值吗? – 2012-01-27 22:17:49
考虑缓存这些图像(以便您不必每次都查询)。 – 2012-01-27 22:18:58