title: How can I correctly calculate the time delta?
body: <p>I'm trying to create a game with an independent frame rate, in which <code>myObject</code> is moving to the right at one unit per millisecond. However, I don't know how to calculate <code>deltaTime</code> in this code:</p>
  
  <pre><code>var currentTime = 0;
  var lastTime = 0;
  var deltaTime = 0;
  
  while( play ) {
  
      // Retrieve the current time
      currentTime = Time.now();            
      deltaTime = currentTime - lastTime;  
      lastTime = currentTime;
  
      // Move myObject at the rate of one unit per millisecond
      myObject.x += 1 * deltaTime;       
  }
  </code></pre>
  
  <p>Let's say the first frame took 30 ms, so <code>deltaTime</code> should be 30 but it was 0
  because we only know the time at the start of the frame not at the end of the frame. Then, in the second frame it took 40 ms, so <code>deltaTime</code> is 30 and thus <code>myObject.x</code> is 30. However, the elapsed time is 70 ms (30ms in 1st frame + 40ms in 2nd frame ) so <code>myObject.x</code> is supposed to be 70, not 30.</p>
  
  <p>I'm not simulating physics, I'm just trying to move <code>myObject</code> relative to the elapsed time (not the frame).</p>
  
  <p>How do I calculate <code>deltaTime</code> correctly?</p>
  
  <p>I know that some game engine people use chunk of time or tick, so they're animating ahead of time. Also, I've already read Glenn Fiedler's <a href="http://gafferongames.com/game-physics/fix-your-timestep/" rel="nofollow">article on fixing your timestep</a> and many other ones, but I'm still confused.</p>