title: Call or cancel timer event angular 2
body: <p>I basically have a class that should subscribe to <code>commModel.sessionState$</code> and if <code>isValid</code> is true it should cancel the timer and recreate it again. The problem is that when the timer fires I get this error: <code>TypeError: Cannot read property 'setSessionState' of undefined</code>, but when debugging in the constructor <code>commModel</code> is OK. Also the console.log in the subscribe does not execute. Where is the problem and how can I fix it ?</p>
  
  <pre><code>import { Injectable } from '@angular/core';
  import { CommonModel } from '../core/store/common.model';
  import { Observable } from 'rxjs/Observable';
  
  import 'rxjs/add/observable/interval';
  import 'rxjs/add/operator/publish';
  import 'rxjs/add/operator/startWith';
  
  @Injectable()
  export class SessionTimeoutHandler {
  
    sessionState: Observable&lt;boolean&gt;;
    timeoutCountdown$: Observable&lt;boolean&gt;;
  
    timer: any;
  
    constructor(private commModel: CommonModel) {
      this.sessionState = commModel.sessionState$;
      this.sessionState.subscribe(
        isValid =&gt; {
          console.log('test',isValid);
          if(isValid) {
            clearTimeout(this.timer);
            this.timer = setTimeout(this.invalidateSessionState, 5 * 1000);
          }
        }
      )
  
      this.timer = setTimeout(this.invalidateSessionState, 5 * 1000);
    }
  
    invalidateSessionState() {
      this.commModel.setSessionState(false);
    }
  
  }
  </code></pre>