1

I was using a timer solution from https://stackoverflow.com/a/39664796/2122303 that works quite well, except that for my case, I want it to be stopped when the users puts the app in the background.

export class Component implements OnInit, OnDestroy {

  private tick: string;
  private subscription: Subscription;

  constructor() {
  }

  ngOnInit() {
    let timer = TimerObservable.create(2000, 1000);
    this.subscription = timer.subscribe(t => {
      this.tick = t;
    });
  }

  ngOnDestroy() {
    this.subscription.unsubscribe();
  }
}

So I added in the same class

public onDeviceReady() {
    document.addEventListener("pause", this.onPause, false);
}
public onPause() {
    console.log("device in Pause at ", this.tick)
    this.subscription.unsubscribe();
}

And The code compiles fine, but when the device goes to Pause, I get the error: Uncaught TypeError: Cannot read property 'unsubscribe' of undefined

Why does onPause not see my this.subscription ?

Community
  • 1
  • 1
  • What happens if you just check if this.subscription is undefined? It seems pause is executed before ngOnInit – misha130 May 14 '17 at 18:28

0 Answers0