6

I have a problem. I'd like to reload website after 5 minutes and that is my question, how to reload website in Typescript/Angular application?

akaoD
  • 91
  • 1
  • 1
  • 7
  • You could use `setTimeout` and then use the Router call your home route with a random parameter, which makes the site reload. But why would you need to reload? Couldn't you use WebSockets? – Jannis Lehmann Feb 26 '18 at 14:08
  • 1
    Possible duplicate of [How to reload a page using JavaScript?](https://stackoverflow.com/questions/3715047/how-to-reload-a-page-using-javascript) – Heretic Monkey Feb 26 '18 at 14:11

2 Answers2

13

In your main app component file, under ngOnInit(), you could add a setTimeout method that reloads every 5 minutes like so:

ngOnInit() {
  setTimeout(() => {
    window.location.reload();
  }, 300000); // Activate after 5 minutes.
}

Though I don't know your situation, I would highly advise against doing this. Most likely, whatever you are trying to accomplish, can probably be done in a better way than refreshing the page. That's just my opinion. For me as a User, i'd be really annoyed if i'm browsing a site and it just randomly refreshes.

David Anthony Acosta
  • 4,766
  • 1
  • 19
  • 19
  • Is there a way for me to set specific date? For example I want the page to be able to reload at 14 o'clock. –  Mar 07 '20 at 12:55
  • It's not set timeout it setInterval so that , this will work on each 5 minutes interval ngOnInit() { setInterval(() => { window.location.reload(); }, 300000); // Activate after 5 minutes. } – Arul Oct 04 '21 at 07:29
2

use window.location.reload(); to refresh the page

Ricardo
  • 2,427
  • 19
  • 35