0

i am trying to monitor a close event for a tab opened by window.open() using setInterval(). when I tried to clearInterval in ngOnDestroy() it alway telling me the timmer is undefined. An suggestions?

timmer: any;
newWindow: any;

ngOnDestroy() {
    this.newWindow.close(); // newWindow is always undefined...
    clearInterval(this.timmer) // timmer is always undefined...
  }

// this I am calling an api to get the new window url and use window.open to open it...

public openWindow() {
    this.claimsService.getUrl(this.id).subscribe(
      response => {
        this.newWindow = window.open(response.url);
        this.timmer = setInterval(() => {
          if (newWindow.closed) {
            clearInterval(loop);
          }
      },
      error => {
        //...error handler...
      }
    );
  }
gary wu
  • 1
  • 1
  • 1
    are you calling openWindow more than once. Its always a good practice to clearInterval before assigning inside your function – joyBlanks Sep 06 '19 at 20:12
  • thanks for the quick reply! openWindow only been called once. – gary wu Sep 06 '19 at 20:17
  • What scope timeer is in? Looks like that this pointing actually to different object. Is it part of some object or it is in global scope? – j2ko Sep 06 '19 at 20:20
  • Possible duplicate of [How to properly do a "bind" in angular2 typescript?](https://stackoverflow.com/questions/45136680/how-to-properly-do-a-bind-in-angular2-typescript) – j2ko Sep 06 '19 at 20:25
  • @j2ko it's in global scope – gary wu Sep 06 '19 at 20:27
  • @garywu so you don't need `this` at all. right? – j2ko Sep 06 '19 at 20:31
  • looks like it's a scope issue that setInterval cannot get global variable updated – gary wu Sep 06 '19 at 21:48

0 Answers0