Nearly all the SetInterval errors I've seen here on StackOverflow are due to passing a function name as a "string," but maybe I am still missing some sort of variable scope problem. Please advise!
I'm creating a slideshow with pause and play capabilities. When playing, I want the slides to advance every 3 seconds. But the SetInterval to launch the NextSlide function is failing after executing once.
I've tried it as...
SetInterval("nextSlide()", 3000)
SetInterval(nextSlide(), 3000)
var t = SetInterval(nextSlide(), 3000)
var t = SetInterval(function(){nextSlide(), 3000)
... with failure every time. What am I missing here?
var slide_1 = "slide_1";
var slide_2 = "slide_2";
var slideNum = 0;
var odd = true;
var totalMax = 6;
var busy = false;
var allSlides = new Array();
allSlides[0] = "test_01";
allSlides[1] = "test_02";
allSlides[2] = "test_03";
allSlides[3] = "test_04";
allSlides[4] = "test_05";
allSlides[5] = "test_06";
allSlides[6] = "test_07";
function PlaySlide(){
var t = SetInterval(nextSlide(),3000)
document.getElementById("play").style.visibility = "hidden";
document.getElementById("pause").style.visibility = "visible";
}
function nextSlide(){
if(slideNum < totalMax && !busy){
busy = true
document.getElementById("loading").style.zIndex = 4;
slideNum = slideNum + 1
var slide = allSlides[slideNum]
var link = "https://dl.dropboxusercontent.com/u/..." + slide + ".jpg"
odd = !odd
if(odd){document.getElementById(slide_1).src = link} //which <img>.onLoad lauches a fadeIn()
else{document.getElementById(slide_2).src = link}
}
}
var t = SetInterval(nextSlide(),3000)
^ ^^
You have 2 problems
There is no SetInterval, the s is lowercase. Second, you are calling the function nextSlide(), not assigning it
var t = window.setInterval(nextSlide, 3000);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With