title: Access to class variable in setTimeout function
body: <p>I have a JavaScript class that has to publish events every so often. The code looks like this : </p>
  
  <pre><code>class TimerService {
    constructor(BroadcastService) {
      this.Broadcast = BroadcastService;
      this.timeout;
      this.warningTimer;
      this.startTimer();
    }
  
    startTimer() {
      clearTimeout(this.timeout);
      clearTimeout(this.warningTimer);
      this.timeout = setTimeout(this.startWarning, 30000);
    }
  
    startWarning() {
      this.Broadcast.send("timeout-warning");
      this.warningTimer = setTimeout(this.logout, 60000);
    }
  
    logout() {
      this.Broadcast.send("session-expired");
    }
  }
  
  export default { service: TimerService, name: "TimerService" };
  </code></pre>
  
  <p>The issue is, in the setTimeout calllback functions, the <code>this</code> scope points to <code>window</code> so I am not able to access <code>this.Broadcast</code>. What is the best way to structure this so I have access to everything I need?</p>