-1

I have an angular 9 app that is mostly used by people on phones. The page has live data on it. Is there an event I can listen to when someone reopens the page so that I can refresh data?

I've noticed that if I leave the page opened in a browser, and then minimize the browser or put the phone to sleep the page will show old data until the regular refresh timers hit. I would like to get an event as soon as someone resumes the page or reactivates it again so I can instantly refresh data.

Is there such an event that I can listen to?

JensB
  • 6,663
  • 2
  • 55
  • 94
  • 1
    Does this answer your question? [JavaScript / jQuery: Test if window has focus](https://stackoverflow.com/questions/3479734/javascript-jquery-test-if-window-has-focus) – Eldar May 18 '20 at 18:17
  • It points me in the general direction, but I have not yet found an angular way of doing this. – JensB May 18 '20 at 18:55

1 Answers1

2

Well, implementing the solution mentioned in the comments in an angular way is just implementing a HostListener like below :

export class AppComponent {
  name = "Angular";
  focusChanges = [];

  @HostListener("window:focus")
  protected onFocus() {
  // this is where you may want to reload your data
    this.focusChanges.push(new Date().toLocaleTimeString());
  }
}

Stackblitz

Eldar
  • 9,781
  • 2
  • 10
  • 35