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 ?

1
  • What happens if you just check if this.subscription is undefined? It seems pause is executed before ngOnInit Commented May 14, 2017 at 18:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.