Here is my html:
<div class="slider_container Centered">
<figure id="Slider">
<img src="Images/HBC.jpg" id="1" class="effect Image"/>
<img src="Images/Easter-Pic.jpg" id="2" class="effect Image"/>
</figure>
</div>
and my Javascript:
var Figure_Slider;
var Images;
var Image_Num;
var Current_Element;
var Previous_Element;
function main()
{
Figure_Slider = document.getElementById("Slider");
Images = document.getElementById("Slider").children;
Image_Num = Images.length;
for (var i = 1; i <= Image_Num; i++)
{
var index = i;
Current_Element = document.getElementById(i.toString());
if (index == 1)
{
Current_Element.classList.add("active");
}
else
{
Previous_Element.classList.remove("active");
Current_Element.classList.add("active");
}
window.setTimeout(function(){advance();}, 4000); //This doesn't work
advance();
}
Current_Element.classList.remove("active");
}
function advance()
{
Previous_Element = Current_Element;
}
window.onload = main;
I'm trying to make an image slider. When I use the Chrome debug tools, and step through the javascript manually, it works fine. When I just run it straight, the timer is not executed. What is wrong with the setTimeout
function? I've seen many examples of its output not being given to a variable, so that's not it. I even did the anonymous function fix. The advance();
call underneath the setTimeout
is just temporary so I could view the logic. Without the advance();
line, the script drops an unhandled event where the Previous_Element
has no class named active to remove. Therefore, both parts of the timer statement (delay, and function execution) don't happen. Other than that, the code works fine. Let me know if you need the CSS. I've tried some of the fixes:
function wait(delay)
{
setInterval(advance(), delay);
}
Then calling that function in the loop, but it locks up the browser using 100% or more of my CPU. Is there another way of doing this?
Thanks in advance.