简体   繁体   English

JavaScript数组无法识别称为变量

[英]Javascript array is not recognizing called variable

function slideShow() {
    var pageSplash = document.getElementById('splash');
    var image = ["pic1.jpg", "pic2.jpg", "pic3.jpg", "pic4.jpg"];
    var i = 0;

    while (i <= image.length) {
        if (i > image.length) {
            i = 0;
        }

        i += 1;
        pageSplash.innerHTML = '<img id ="splashImage" src="file:///C:/JonTFS/JonGrochCoding/Javascript%20Practical%20Test/' + image[i] + '">';
        setTimeout('slideShow', 5000);
    }

}

I'm unsure why my i variable is not being recognized as the i variable from the rest of the function, so when ever I try to run my while loop it get's an error message saying that it's undefined. 我不确定为什么我的i变量在该函数的其余部分中未被识别为i变量,因此,每当我尝试运行while循环时,都会收到一条错误消息,指出它是未定义的。

I think you want setInterval instead of setTimeout , and you want you be careful that you increment i after you you update innerHTML . 我认为您要使用setInterval而不是setTimeout ,并且要小心在更新innerHTML之后递增i

function slideShow() {
    var pageSplash = document.getElementById('splash');
    var image = ["pic1.jpg", "pic2.jpg", "pic3.jpg", "pic4.jpg"];
    var i = 0;

    setInterval(function () {
      if (i === image.length) { 
          i = 0;
      }
      pageSplash.innerHTML = '<img id ="splashImage" src="file:///C:/JonTFS/JonGrochCoding/Javascript%20Practical%20Test/' + image[i] + '">';
      i++;
    }, 5000)
}

slideShow();

You don't need a while loop. 您不需要while循环。 You don't need to reset i . 您无需重置i You don't need to set innerHTML . 您不需要设置innerHTML

Click Run code snippet... to see how this works. 点击运行代码段...以了解其工作原理。 More explanation below the code 代码下方有更多说明

 function slideShow(elem, images, delay, i) { elem.src = images[i % images.length]; setTimeout(function() { slideShow(elem, images, delay, i+1); }, delay); } // setup slideshow 1 slideShow( document.querySelector('#slideshow1 img'), // target element [ // array of images 'http://lorempixel.com/100/100/animals/1/', 'http://lorempixel.com/100/100/animals/2/', 'http://lorempixel.com/100/100/animals/3/', 'http://lorempixel.com/100/100/animals/4/', 'http://lorempixel.com/100/100/animals/5/', 'http://lorempixel.com/100/100/animals/6/' ], 1000, // 1000 ms delay (1 second) 1 // start on slide index 1 ); // setup slideshow 2 slideShow( document.querySelector('#slideshow2 img'), // target element [ // array of images 'http://lorempixel.com/100/100/nature/1/', 'http://lorempixel.com/100/100/nature/2/', 'http://lorempixel.com/100/100/nature/3/', 'http://lorempixel.com/100/100/nature/4/', 'http://lorempixel.com/100/100/nature/5/', 'http://lorempixel.com/100/100/nature/6/' ], 500, // 500 ms delay 1 // start on slide 1 ); 
 #slideshow1, #slideshow2 { width: 150px; display: inline-block; } 
 <div id="slideshow1"> <h2>Animals</h2> <p>(1000 ms delay)</p> <!-- initial image --> <img src="http://lorempixel.com/100/100/animals/1/"> </div> <div id="slideshow2"> <h2>Nature</h2> <p>(500 ms delay)</p> <!-- initial image --> <img src="http://lorempixel.com/100/100/sports/1/"> </div> 

This is a huge improvement because your slideshow function is reusable. 这是一个巨大的改进,因为您的slideshow功能可以重复使用。 It means you can use the same function for any slideshow you want. 这意味着您可以对所需的任何幻灯片演示使用相同的功能。 You can even run multiple slideshows on the same page, as I have demonstrated here. 正如我在此处演示的那样,您甚至可以在同一页面上运行多个幻灯片。

setTimeout calls the function again so you're re-initializing i to 0 every time you call it. setTimeout再次调用该函数,因此每次调用i时都会将其重新初始化为0。 Since you can use setTimeout to call the function recursively you don't need the while loop. 由于可以使用setTimeout递归调用函数,因此不需要while循环。 Pull i out of the function altogether and make it a global variable. 将i完全从函数中拉出并使其成为全局变量。

//i should be global 
var i = 0;

function slideShow() {
var pageSplash = document.getElementById('splash');
var image = ["pic1.jpg", "pic2.jpg", "pic3.jpg", "pic4.jpg"];

    if (i >= image.length) {
        i = 0;
    }

    i += 1;

    pageSplash.innerHTML = '<img id ="splashImage" src="file:///C:/JonTFS/JonGrochCoding/Javascript%20Practical%20Test/' + image[i] + '">';

    //set timeout is going to call slideShow again so if it's in the function it will call recursively, if you wanted to stop after a certain point you could nest setTimeout in an if
    setTimeout(slideShow, 5000);
}

//you need to initially call the function
slideShow();

As others have pointed out, the while loop is unnecessary and, as I pointed out, the setTimout was incorrectly written. 正如其他人指出的那样,while循环是不必要的,并且正如我所指出的那样,setTimout的编写是错误的。 The following simplifies your code significantly: 以下内容大大简化了您的代码:

 var i = 0;
 function slideShow() {
     var pageSplash = document.getElementById('splash');
     var imageArray = ["pic1.jpg", "pic2.jpg", "pic3.jpg", "pic4.jpg"];

     if(i < imageArray.length) {
         pageSplash.innerHTML = '<img title='+ imageArray[i] + ' id ="splashImage" src="file:///C:/JonTFS/JonGrochCoding/Javascript%20Practical%20Test/' + imageArray[i] + '">';
     }
     i++;
 }

 setInterval(slideShow, 2000);

See: https://jsfiddle.net/dauvc4j6/8/ for a working version. 有关工作版本,请参见: https//jsfiddle.net/dauvc4j6/8/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM