title: Javascript while setTimeout
body: <p>I want to make a GreaseMonkey script. I want to have a <code>setTimeout</code> at the end of my while, but I don't know how to do that.</p>
  
  <pre><code>run();
  function run(){
    var klovas = document.getElementById("light").innerHTML;
    var btn = document.getElementsByClassName("farm_icon farm_icon_a");
    if(klovas &lt; 6){
      alert("Kevés egység van");
    } else {
      var i = 0;
      while (i &lt; btn.length){
        if(typeof btn[i] != "undefined"){
          btn[i].click();
        }
      i++;
      setTimeout("run()", 3000);
      }
    }
  }
  </code></pre>
  
  <p>With this code, the problem is that the <code>setTimeout</code> is not working and doesn't wait 3 seconds like it is supposed to.</p>
  
  <p>I tried other ways, but nothing has worked.</p>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>function run(){
    var klovas = document.getElementById("light").innerHTML;
    var btn = document.getElementsByClassName("farm_icon farm_icon_a");
    if(klovas &lt; 2){
      alert("Kevés egység van");
    } else {
      var i = 0;
      while (i &lt; btn.length){
        if(typeof btn[i] != "undefined"){
          btn[i].click();
        }
      i++;    
      }
    }
  }
  setInterval(run, 6000); 
  </code></pre>
  
  <p>I tryed this. Its runing every 6 sec, but i get error in website, that i cand click more than 5 times in a sec. So waiting 6secound when i open the page, and after click, and i get error. Its not jet working. :(</p>