概述
此答案分为两部分:帮助您发布实际代码,然后推荐另一种方法
帮助处理您发布的代码
这段代码:
for (var i in images) {
setTimeout(fadeInOut(i, images), 5000);
//alert(i);
}
...有一个主要问题和一个次要问题:
-
主要问题:您正在调用fadeInOut 函数并将其返回值 传递给setTimeout,就像您在其他任何时候执行function1(function2(arg, arg2)); 一样.相反,将setTimeout 行更改为:
setTimeout(createFader(i, images), 5000);
...并添加一个如下所示的createFader 函数:
function createFader(index, array) {
return function() {
fadeInOut(index, array);
};
}
createFader 函数创建一个函数,当调用该函数时,将使用您传递给它的参数调用 fadeInOut。你不能这样做:
setTimeout(function() { // <== WRONG, WILL NOT WORK
fadeInOut(i, images);
}, 5000);
...因为那里的匿名函数(这是一个闭包)将获得对i 和images 的持久引用,所以当超时时发生时,它将使用它们的值 then,而不是创建函数时的值。更多:Closures are not complicated
(不要使用带有setTimeout 的字符串作为另一个建议建议的答案。)
for..in 用于循环遍历对象的可枚举属性名称,not 用于循环遍历数组索引。在琐碎的环境中,它主要是有效的,但如果您不完全了解自己在做什么,这是一个会咬你的坏习惯。更多信息:Myths and realities of for..in 如果您使用的是 jQuery,并且可以有几个函数调用的开销,$.each 非常适合遍历数组(除了它不适合稀疏数组,但您的不是t 稀疏)。
另外,请注意new Array() 最好写成[],实际上您可以将条目直接放在方括号中,而不是事后进行赋值。当然,它是 document(全部小写),而不是大写 D 的 Document。
所以把这些放在一起:
$(document).ready(function () {
var images = [
"#bannerImageOne",
"#bannerImageTwo"
];
var i;
$('#homeCarousel img').hide();
for (i = 0; i < images.length; ++i) {
setTimeout(createFader(i, images), 5000);
}
});
function createFader(index, array) {
return function() {
fadeInOut(index, array);
};
}
function fadeInOut(i, images) {
$(images[i]).fadeIn("slow").delay(2000).fadeOut();
//alert(images[i]);
}
另外请注意,我将 i 的声明移到了函数的顶部,因为无论如何 JavaScript 引擎都会看到它(详情:Poor, misunderstood var)。
除非你有充分的理由说明 fadeInOut 必须是一个全局函数,否则我会将它(和 createFader)移动到你传递给 ready 的函数中,这样我们就不会在全部。这也让他们都可以直接使用images,所以我们不必传递数组引用(不是那很昂贵,这完全没问题)。看起来像这样:
$(document).ready(function () {
var images = [
"#bannerImageOne",
"#bannerImageTwo"
];
var i;
$('#homeCarousel img').hide();
for (i = 0; i < images.length; ++i) {
setTimeout(createFader(i, images), 5000);
}
function createFader(index) {
return function() {
fadeInOut(index);
};
}
function fadeInOut(i) {
$(images[i]).fadeIn("slow").delay(2000).fadeOut();
//alert(images[i]);
}
});
之所以有效,是因为通过将函数放在您传递给ready 的匿名函数中,我们在调用该匿名函数的上下文中使它们闭包。 (参见上面关于闭包的链接。)即使在函数返回后(它几乎立即返回),上下文仍保存在内存中,并为我们的私有数据提供了一个方便的容器。
另一种方式的推荐
由于setTimeout 调用所做的只是将fadeIn 的启动延迟5 秒,因此如果您使用delay,代码会更简单(因为您必须在@ 之间暂停一下) 987654359@和fadeOut):
$(document).ready(function () {
var images = [
"#bannerImageOne",
"#bannerImageTwo"
];
$('#homeCarousel img').hide();
$.each(images, function(index, id) {
$(id).delay(5000).fadeIn("slow").delay(2000).fadeOut();
});
});
或将fadeInOut 保留为单独的函数:
$(document).ready(function () {
var images = [
"#bannerImageOne",
"#bannerImageTwo"
];
$('#homeCarousel img').hide();
$.each(images, function(index, id) {
fadeInOut(id, 5000);
});
function fadeInOut(id, delay) {
$(id).delay(delay).fadeIn("slow").delay(2000).fadeOut();
}
});
delay 取代了setTimeout,所有图像在 5 秒后开始淡入淡出。 (我还提供了一个使用 $.each 遍历数组的示例。)如果您希望它们按顺序淡入,您可以将延迟乘以 index:
$.each(images, function(index, id) {
$(id).delay(5000 * (index + 1)).fadeIn("slow").delay(2000).fadeOut();
});
或
$.each(images, function(index, id) {
fadeInOut(id, 5000 * (index + 1));
});
...这将使第一张图像延迟 5 秒,第二张图像延迟 10 秒,以此类推。