title: Is there a way to run a function for a given amount of time in Node?
body: <p>I'm working on an application in Electron/Node that is supposed to collect some data. 
  Data is streamed to a file using (pseudo-code)</p>
  
  <pre><code>function collectData(data) {
  file.write(data+"\n");
  }
  </code></pre>
  
  <p>Which is fired by an event</p>
  
  <pre><code>app.on('ready', (event, args) =&gt; {
    collectData();
  })
  </code></pre>
  
  <p>How can I make sure the event calls the function for a given amount of time? Is there a cleaner and more efficient solution than defining a start and end time, and checking for <code>end &gt; start</code> each loop?</p>