我目前正在放置一个小幻灯片,自动移动淡出背景图像的东西。 (不透明度。) 我的问题是我正在尝试使用变量来存储要运行的代码,因为setTimeout很顽固,并且不会使用括号中的任何内容运行。 (我需要使用它们,否则我的代码会变得非常混乱......) 我现在拥有的是
imgID = 0;
// window.setInterval(nextSLIDE, 1000);
nextSLIDE();
function nextSLIDE( step2 ) {
slideVAR = "slide" + imgID;
window.setTimeout(imgIDchange(), 50);
test2 = window.setTimeout(changeOpacityNINE);
tes5t = window.setTimeout(changeOpacity8, 100); // If you are wondering about the irradical names, that is because I made them all non-unique earlier, and I got lazy, so I made them unique..
test4 = window.setTimeout(changeOpacity7, 200);
test6 = window.setTimeout(changeOpacity6, 300);
tes6t = window.setTimeout(changeOpacity5, 400);
twest = window.setTimeout(changeOpacity4, 500);
testt = window.setTimeout(changeOpacity3, 600);
testyt = window.setTimeout(changeOpacity2, 700);
teswt = window.setTimeout(changeOpacity1, 800);
}
function imgIDchange() {
imgID = imgID + 1;
}
function changeOpacity( opacity ) {
document.getElementById("headerIMG").style.opacity = opacity;
}
var changeOpacityNINE = changeOpacity(0.9);
var changeOpacity8 = changeOpacity(0.8);
var changeOpacity7 = changeOpacity(0.7);
var changeOpacity6 = changeOpacity(0.6);
var changeOpacity5 = changeOpacity(0.5);
var changeOpacity4 = changeOpacity(0.4);
var changeOpacity3 = changeOpacity(0.3);
var changeOpacity2 = changeOpacity(0.2);
var changeOpacity1 = "changeOpacity(0.1);"
var FULL = changeOpacity(1)

我正在寻找一种方法来实现它
A)工作,而不是运行变量。
B)或者找一些其他的工作......
如果我的设计那可怕,让你的眼睛流血,请随时告诉我如何重做它,但我宁愿不重做一般。 (我很懒..)
答案 0 :(得分:4)
如果我理解正确,你想用函数调用setTimeout
并传递参数吗?
如果是这样,您只需将参数添加到setTimeout
电话的末尾即可。所以如果你想打电话
changeOpacity(0.5);
1000毫秒后,你会像这样使用setTimeout
:
setTimeout(changeOpacity, 1000, 0.5);
答案 1 :(得分:3)
使用setTimeout,参数如下:
setTimeout(callback,delay,args);
所以你可以这样做:
setTimeout(changeOpacity,*DELAY*,0.7); // etc...