ReferenceError SetInterval not defined
10,884
Solution 1
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);
Solution 2
Please use "setInterval"
instead of "SetInterval" and see result.
Author by
user3107660
Updated on June 15, 2022Comments
-
user3107660 almost 2 years
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} } }
-
VisioN over 10 yearsJavaScript is case sensitive language. This method is called
setInterval
. -
gen_Eric over 10 yearsP.S. It should just be
setInterval(nextSlide, 3000);
. Don't pass strings, pass functions. Also, you want to pass the function, not call it and pass its return value. You can also do:setInterval(function(){ nextSlide(); }, 3000);
.
-
-
user3107660 over 10 yearsbeautiful. Simply beautiful!