gpt4 book ai didi

javascript - 如何每 x 秒显示一个新图像?

转载 作者:行者123 更新时间:2023-11-30 15:43:38 25 4
gpt4 key购买 nike

尝试使用 JavaScript 设置间隔计时器,我不确定如何将图像放入数组中。
我尝试了几种不同的方法,但无法正常工作。

var myImage = document.getElementById("mainImage");

var imageArray = [img class="irc_mi ix3z7xWjrSdc-pQOPx8XEepE" alt="Image result for baseball images" style="margin-top: 70px;" src="http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg" onload="google.aft&&google.aft(this)" width="626" height="415",
img class="irc_mi ix3z7xWjrSdc-pQOPx8XEepE" alt="Image result for baseball images" style="margin-top: 70px;" src="http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg" onload="google.aft&&google.aft(this)" width="626" height="415",
img class="irc_mi ix3z7xWjrSdc-pQOPx8XEepE" alt="Image result for baseball images" style="margin-top: 70px;" src="http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg" onload="google.aft&&google.aft(this)" width="626" height="415"]

var imageIndex = 0;

function changeImage() {
myImage.setAttribute("src",imageArray[imageIndex]);
imageIndex++;
if(imageIndex >= imageArray.length) {
indexImage = 0;
}
}

setInterval(changeImage,5000);

在此先感谢,我也是新手,确实尝试寻找答案但找不到。

最佳答案

几件事

  1. 您的数组需要是字符串数组:

  2. 您将 src 属性设置为包含多个源的整个字符串

  3. indexImage 未定义

    var myImage = document.getElementById("mainImage");

    var imageArray = [
    "http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg",
    "http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg",
    "http://totalsportscomplex.com/wp-content/uploads/2014/09/baseball-pic.jpg"
    ]

    var imageIndex = 0;

    function changeImage() {
    myImage.setAttribute("src", imageArray[imageIndex]);
    imageIndex++;
    if (imageIndex >= imageArray.length) {
    imageIndex = 0;
    }
    }

    setInterval(changeImage, 5000);

关于javascript - 如何每 x 秒显示一个新图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40425604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com