Search code examples
javascriptfor-loopsettimeoutgetelementsbyclassname

How would I get to increment the set timeout to apply to each classname pause?


I need help trying to increment my for loop so after 8 seconds my divs show not just one. I would prefer vanilla js if possible.

I have tried doing my own for loop and settimeout I have failed.

var googleAds = document.getElementsByClassName('pause');
var i;
for(var i = 0; i < googleAds.length; i++) { 
    setTimeout(function () {
    googleAds[i].style.display = "block";
}, 8000);
 }

isplaying after 8 seconds


Solution

  • You need to use the for loop inside the anonymous function for i to be in scope:

    setTimeout(function () {
      for(var i = 0; i < googleAds.length; i++) { 
        googleAds[i].style.display = "block";
      }
    }, 8000);