此代码中的字符串是什么意思,它是交通灯脚本的一部分,我不知道每条线路的作用。
var index = 0;
var variableLen = variable.length;
function nextvariableClick() {
index++;
if (index == variableLen)
index = 0;
var image = document.getElementById('starting_light');
image.src = variable[index];
答案 0 :(得分:1)
variable
似乎是一个存储图像引用(URI或路径)的数组,它被提供给图像元素的src
属性<img>
。简单的脚本执行以下逻辑:
index
增加1 variable
index
数组的第n个元素
智能猜测是这是一个图像循环功能。调用nextvariableClick
后,它会按照它们在variable
数组中的显示顺序循环显示图像列表。
由于脚本非常简单,因此查看其功能的最佳方法是构建功能代码段:
// Define dummy image references
var variable = [
'https://placehold.it/500x300/e41a1c/ffffff',
'https://placehold.it/500x300/377eb8/ffffff',
'https://placehold.it/500x300/4daf4a/ffffff',
'https://placehold.it/500x300/984ea3/ffffff',
'https://placehold.it/500x300/ff7f00/ffffff'
];
/* Start of code you have provided */
var index = 0;
var variableLen = variable.length;
function nextvariableClick() {
index++;
if (index == variableLen)
index = 0;
var image = document.getElementById('starting_light');
image.src = variable[index];
}
/* End of code you have provided */
// We execute the function to start initialise it, and set a starting image
nextvariableClick();
window.setInterval(nextvariableClick, 1000);
&#13;
<p>The function is called at runtime, and called every 1s. You should see cycling of image sources.</p>
<img id="starting_light" src="" />
&#13;