Angular 2: How to refresh entire page automatically on after some time

10,487

Solution 1

try this

setTimeout(
function(){ 
location.reload(); 
}, 10000);

Solution 2

you have two choices:

1- Using ng2-simple-timer-example:

Angular 2 has a simple component that handle timer: ng2-simple-timer-example

Github Link

This plunker shows the result:

Online Plunker

2- setTimout:

As @Araivnd mentions you can using ngOnInit(angular calls ngOnInit after creating the component) and setTimout as following:

ngOnInit(){
    setTimeout(function(){
          //code here
        },10000);
}
Share:
10,487

Related videos on Youtube

PAR
Author by

PAR

Updated on June 04, 2022

Comments

  • PAR
    PAR almost 2 years

    I am trying to code in Angular to such that it reloads entire HTML page automatically after 10 seconds.

    Please help how to do it, i am new to Angular 2.

    Thanks in advance.

    • Aravind
      Aravind over 6 years
      use setTimout() and call the ngOnInit()
    • DeborahK
      DeborahK over 6 years
      Could you elaborate on the purpose for this? Do you want to reload the entire page? Or just the data for the page? The solution is different depending on which you require.
    • PAR
      PAR over 6 years
      @Deorahk yes,In html page i am having calendar which contain events,once user clicked on that it ,it should block for 10 minutes and call the services again after 10 minutes and reload.
    • DeborahK
      DeborahK over 6 years
      Check out the answer to this question: stackoverflow.com/questions/35316583/… It goes into detail on how to set up a timer for this.
    • Rahul Singh
      Rahul Singh over 6 years
      Possible duplicate of Angular2 http at an interval