Angular / Javascript – Do something after a time elapse

Let’s say you want to change a property, or have something occur on your Angular page after the page loads initially. The ‘setTimeout()’ is useful here.

(ts file)
export class MyComponent implements OnInit
  showAdvert = false;

  setTimeout(() => {
    this.showAdvert = true;
    }, 5000);

In your html file

//I'm using square brackets where you need 
//angle brackets because I'm fighting against wordpress.
[div]This is the main div
  [div [disabled]='!showAdvert' ]This is an annoying ad pop up box that appears 5 seconds after the page is 
  initially loaded because the value of 'showAdvert' changes at that time.

So you have a div that isn’t initially showing because it is disabled, however 5 seconds after the constructor runs, the box will appear because ‘showAdvert’ becomes true.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s