Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

javascript - Slideshow error "RangeError: Maximum call stack size exceeded

I am trying to add this image slideshow into my website, However keeping running into a MAximum call stack size exceeded. The slider runs through the images not in the time interval set and then the error occurs

var i = 0;          // Start Point
var images = [];    // Images Array
var time = 3000;    // Time Between Switch
     
// Image List
images[0] = "http://lorempixel.com/400/200/animals";
images[1] = "http://lorempixel.com/400/200/sports";
images[2] = "http://lorempixel.com/400/200/food";
images[3] = "http://lorempixel.com/400/200/people";

// Change Image
function changeImg(){
    document.slide.src = images[i];

    // Check If Index Is Under Max
    if(i < images.length - 1){
      // Add 1 to Index
      i++; 
    } else { 
        // Reset Back To O
        i = 0;
    }

    // Run function every x seconds
    setTimeout("changeImg()", time);
}

// Run function when page loads
window.onload=changeImg;

question from:https://stackoverflow.com/questions/65886262/slideshow-error-rangeerror-maximum-call-stack-size-exceeded

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am posting this as an answer to have a minimal, complete and verifiable example (mcve) to make sure that the slider is not the culpritd, the code will not give that error.

Here is current syntax - if that still gives an error then there is certainly something else wrong

let i = 0, img;
const images = ["https://via.placeholder.com/150/0000FF/808080?text=Image1",
    "https://via.placeholder.com/150/FF0000/FFFFFF?text=Image2",
    "https://via.placeholder.com/150/FFFF00/000000?text=Image3",
    "https://via.placeholder.com/150/000000/FFFFFF/?text=Image4"
  ];

const changeImg = function() {
  slide.src = images[i];
  i++;
  if (i >= images.length) i = 0;
}

window.addEventListener("load", function() {
  img = document.getElementById("slide");
  changeImg();
  setInterval(changeImg, 2000);
})
<img id="slide" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...