title: Making a function that changes a variable every 3rd second
body: <p>I'm making a small game, and i want to make some dots that changes their x-position every 3rd second. My broblem is that it only prints out the first value. 
  Im trying to make it as simple as possible.</p>
  
  <p>I tried console.log(i), and figured that the loop worked fine, but the randomX var doesnt change more than once.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var randomX = 0;
  var i = 0, spawnXCount = 10;
  
  function makeRandomXPosition(){
  
   i++;
   if (i &lt; spawnXCount){
    setTimeout(makeRandomXPosition, 3000);
    
   }
   randomX = Math.floor(Math.random()*1000)+1;
  }
  
  
  makeRandomXPosition();
  console.log(randomX);</code></pre>
  </div>
  </div>
  </p>