title: Randomly position divs that fade in and out
body: <p>I have been looking and trying to solve this problem for a while now and I have no idea.</p>
  <p>Here is my react function as it stands.</p>
  <p>The code in bold is the function in question, right now how it works, is that the function iterates through the videos array, then positions those elements randomly, at different times due to the setInterval.</p>
  <pre><code>export default function RandomVideos(){
  useEffect(() =&gt; {
      const videos = Array.from(document.getElementsByClassName('video_container') as HTMLCollectionOf&lt;HTMLElement&gt;);
      const Width = (document.getElementById('container') as HTMLElement).offsetWidth;
      const Height = (document.getElementById('container') as HTMLElement).offsetHeight;
      function fadeIn(thisVideo: HTMLElement){
          thisVideo.classList.add('fade-in');
          thisVideo.classList.remove('fade-out');  
      }
      function fadeOut(thisVideo: HTMLElement){
          thisVideo.classList.add('fade-out');
          thisVideo.classList.remove('fade-in');  
      }
      function randomlyPlaced(){
          videos.forEach(thisVideo =&gt;  {
              setInterval(function() {
                  thisVideo.classList.add(&quot;fade-in&quot;);
                  const randomTop = Math.round(Math.random() * Height) - 20;
                  const randomLeft = Math.round(Math.random() * Width) - 20;
                  thisVideo.style.top = randomTop +&quot;px&quot;;
                  thisVideo.style.left = randomLeft +&quot;px&quot;;
                  fadeIn(thisVideo)
              }, (Math.floor(Math.random() * 7000) + 2000))
          })
      }
      randomlyPlaced()
      
  });
  return(
      &lt;div className=&quot;container&quot; id=&quot;container&quot;&gt;
          {RandomVideosProps.map((video) =&gt; (
              &lt;div className=&quot;video_container&quot; key={video.id}&gt;
                  &lt;video src={video.src}&gt;&lt;/video&gt;
                  {video.text}
              &lt;/div&gt;
          ))}
      &lt;/div&gt;
  );}
  </code></pre>
  <p>The code works as expected, the first iterations of the video's elements fadeIn, then each interval positions itself randomly.
  But I want to elements to fadeOut on each interval, then fade back in. I have no idea how to do this as the interval is inside the forEach method.</p>
  <p>Having the interval outside of the forEach method means the array elements will randomly position altogether, at once. Which is what I am trying to avoid.</p>
  <p>So how I want this code to work - each array element fadesIn, in a random position then fadesOut and fadesIn again at another random position.</p>
  <p>I would love any suggestions or help, I am trying to avoid using jQuery also as I know it is typically not used in react.</p>
  <p>I have not tried using any libraries.</p>
  <p>I am writing in a typescript react file.</p>
  <p>Thanks!</p>