title: setTimeout loop inside setInterval
body: <p>I have code similar to this:</p>
  
  <pre><code>var count = 0;
  
  setInterval(function() {
    if(count===0){
      console.log(123);
      count++;
    } else if(count&gt;0) {
      for(i=1;i&lt;=2;i++){
          (function(ind) {
              setTimeout(function() {
                console.log(ind);
              }, 1000 * ind);
          })(i);
      }
      count=0;
    }
  },1000)
  </code></pre>
  
  <p>The result is not what I expected, what I'd like to achieve for the console log is like:</p>
  
  <pre><code>123
  1
  2
  123
  1
  2
  ...
  </code></pre>
  
  <p>and so on for each interval with 1000ms. Also I'd like to ask is there a better way to do this? The number of setTimeout loop(2 for the above case) can/might be different each time.</p>