使用Javascript变量而不调用函数

Using Javascript Varibles Without Calling Functions In Them

本文关键字:调用 函数 Javascript 变量 使用      更新时间:2023-09-26

我目前正在开发一个小幻灯片,它会自动移动淡入和淡出背景图像。(还有不透明度。)我的问题是,我试图使用变量来存储要运行的代码,因为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)或者找一些其他的工作……

如果我的设计可怕的,并且让你的眼睛流血,请随时告诉我如何我可以重做它,但我宁愿不重做所有的一般。(我很懒…)

如果我理解正确的话,您想用函数调用setTimeout并将参数传递给它?

如果是,您可以简单地将参数添加到setTimeout调用的末尾。如果你想调用

changeOpacity(0.5);

在1000 ms之后,那么您将像这样使用setTimeout:

setTimeout(changeOpacity, 1000, 0.5);

对于setTimeout,参数如下:

setTimeout(callback,delay,args);

所以你可以简单地写:

setTimeout(changeOpacity,*DELAY*,0.7); // etc...