title: Remove or destroy event listener
body: <p>working on <strong>angular2</strong> application.  I have <strong>RxJS timer</strong> instance that will push notification to user when used logged in. But it should not push notification <strong>if tab is not active then scheduler should pause/stop</strong>. It is also done.</p>
  
  <p>But main thing is that I have added that <strong>window (focus, blur )event listener to ngOnInit()</strong>. I have tried to destroy it on <strong>ngOnDestroy()</strong> when we change page/component but though also it does not stop. When we came back to same page then second instance also started of that listener and there will be 2 scheduler instance now in my memory/scope.</p>
  
  <p>So anyone have idea that how to remove/destroy window.listener on <strong>ngOnDestroy</strong> or any other place !</p>
  
  <p>Code:</p>
  
  <pre><code>timerFlag: boolean = true;
  private timer;
  private sub: Subscription;
  
  ngOnInit() {
      this.sub = null;
      this.timer = null;
      console.log("home-init");
      window.addEventListener('blur', this.disableTimer.bind(this), false);
      window.addEventListener('focus', this.initializeTimer.bind(this), false);
  }
  disableTimer() {
      if (this.sub !== undefined &amp;&amp; this.sub != null) {
          this.sub.unsubscribe();
          this.timer = null;
      }
  }
  initializeTimer() {
      if (this.timerFlag) {
          if (this.timer == null) {
              this.timer = Observable.timer(2000, 5000);
              this.sub = this.timer.subscribe(t =&gt; this.runMe());
          }
      }
  }
  runMe() {
      console.log("notification called : " + new Date());
  }
  ngOnDestroy() {
      console.log("Destroy timer");
      this.sub.unsubscribe();
      this.timer = null;
      this.sub = undefined;
      this.timerFlag = false;
      window.removeEventListener('blur', this.disableTimer.bind(this), false);
      window.removeEventListener('focus', this.initializeTimer.bind(this), false);
  }
  </code></pre>
  
  <p>can anyone guide me how to destroy event listener instance so that when I come to same page next time no second instance will start.</p>
  
  <p>I have tried to remove listener in <strong>ngOnInit</strong> as well before start of <strong>window listener</strong>.</p>