title: How can I slow down execution of JavaScript code?
body: <p>I'm writing a script to delete all of my YouTube comments. Each line of this code works to delete one comment, but when I put it into a loop, I get an <code>Uncaught TypeError: Cannot read property 'click' of undefined</code> that doesn't come up when I run each line individually. I'm thinking if I can figure out a way to sleep in between the lines of code I could remove the error.</p>
  
  <pre><code>var myList = document.getElementsByClassName("dropdown-trigger style-scope ytd-menu-renderer");
  for(i = 0; i &lt; myList.length; i++) {
      myList[i].click();
      document.getElementsByClassName("style-scope ytd-menu-navigation-item-renderer")[4].click(); //error here
      document.getElementById("confirm-button").click();
  }
  </code></pre>
  
  <p>I tried using setTimeout, like this:</p>
  
  <pre><code>var myList = document.getElementsByClassName("dropdown-trigger style-scope ytd-menu-renderer");
  for(i=0; i&lt;myList.length; i++) {
      myList[i].click();
      setTimeout(function(){document.getElementsByClassName("style-scope ytd-menu-navigation-item-renderer")[4].click();}, 1000);
      setTimeout(function(){document.getElementById("confirm-button").click();}, 5000);
  }
  </code></pre>
  
  <p>And it returned a number of 279, but no errors, and no comments deleted. What happened?</p>