title: Javascript Canvas Game Delta Time
body: <p>I'm working on a simple javascript canvas based game, everything is going well except for the attempting to get the delta time to calculate. I've followed every tutorial I could find and I still keep getting a 0 for the delta time variable, which results in no animation... please can someone explain to me what I'm doing wrong here</p>
  
  <pre><code>export default class {
    constructor(options) {
  
    // Window Variables
    this.canvas;
    this.ctx;
    this.width;
    this.height;
    this.running = false;
  
    // Time Variables
    this.fps = 30;
    this.timePerTick = 1000/this.fps;
    this.delta = 0;
    this.now;
  this.lastTime = Date.now();
  this.timer = 0;
  this.ticks = 0;
  
  if(options != null) {
  
    if(options.width != null) 
      this.width = options.width;
  
    if(options.height != null)
      this.height = options.height;
  
    if(options.id != null) {
      document.body.innerHTML = "&lt;canvas id='"+options.id+"' width='"+this.width+"' height='"+this.height+"'&gt;&lt;canvas&gt;";
    }
  }
  
  this.canvas = document.getElementById(options.id);
  this.canvas.width = this.width;
  this.canvas.height = this.height;
  
    this.ctx = this.canvas.getContext('2d');
  }
  
  init(callback) {
  
    callback();
    this.running = true;
  }
  
  update(callback) {
  
    if(this.running) {
      this.now = Date.now();
      this.delta = this.now - this.lastTime;
      this.timer += this.delta;
      this.lastTime = this.now;
    }
  
    if(this.timer &gt;= this.timePerTick) {
      this.dt = this.timer/1000;
      callback();
      this.timer = 0;
    }
  
    requestAnimationFrame(() =&gt; {this.update});
  }
  }
  </code></pre>