I am trying to make a slider, and I don’t think setTimeout is working…
<script>
for (c=1;c>0;c++) {
var a = document.getElementsByClassName('sliderimage').length;
for (b=0;b<=a;b++) {
var g = document.getElementsByClassName('sliderimage');
g[b].style.display = 'block';
var h = setTimeout(f,5000);
function f() {g[b].style.display = 'none';};
};
};
</script>
This will set all elements to style block, and then 5 seconds later turn off the last one.
I suspect what you’re actually wanting to do is something more like…
let cur = 0; //Note: I assume the first sliderimage is already set to display 'block'.
let sliders = document.getElementsByClassName('sliderimage');
setInterval(function() {
sliders[cur].style.display = "none";
cur = ++cur % sliders.length;
sliders[cur].style.display = "block";
}, 5000);
setTimeout doesnt tell the script “wait here for 5 seconds, then proceed”, it tells the script “in 5 seconds do this, meanwhile continue doing whatever you’re doing.” (This, for technical reference, is called an asynchronous operation. The main thread of the engine keeps parsing, while a secondary thread holds onto the request and processes it accordingly.)
So it will run through all the loops of b at the same time. (well, one after another, but without pausing)
to give a (sadly) more real world example, if you’ve got any sort of smart assistant in your home, you can tell it “[AssistantName] start a timer for 5 minutes”.
Your script is saying that b times over; the assistant will happily keep setting timers for you, and 5 minutes from now, a royal cocophany goes off as b different timers all start going off at the same time (plus or minus the amount of time it took you to say “start a timer for 5 minutes”).