title: Run a function at the exact beginning of each second (or at a specific number of milliseconds each second)
body: <p>I would like to run a function at the beginning of every second.</p>
  
  <p>With the following:</p>
  
  <pre><code>function loop() {
      console.log('loop', new Date());
  }
  
  setInterval(loop, 1000);
  </code></pre>
  
  <p>I get this: (running with node v11.13.0)</p>
  
  <pre><code>&gt; node loop.js
  loop 2019-04-04T17:37:24.198Z
  loop 2019-04-04T17:37:25.222Z
  loop 2019-04-04T17:37:26.228Z
  loop 2019-04-04T17:37:27.229Z
  loop 2019-04-04T17:37:28.230Z
  loop 2019-04-04T17:37:29.231Z
  loop 2019-04-04T17:37:30.235Z
  loop 2019-04-04T17:37:31.239Z
  </code></pre>
  
  <p>You can see that at each loop, the number of milliseconds is offset by about 200ms, and it is increasing by a few milliseconds each iteration. </p>
  
  <p>Ideally I would want this:</p>
  
  <pre><code>&gt; node loop.js
  loop 2019-04-04T17:37:24.000Z
  loop 2019-04-04T17:37:25.000Z
  loop 2019-04-04T17:37:26.000Z
  ...
  </code></pre>