Skip to main content

angular - How to use setTimeout()

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})


export class AppComponent {
  message: string = "Lorem ipsum dolor sit amet,"
   + "consectetur adipiscing elit. In at ante mattis,"
   + "sollicitudin arcu id, dictum orci."
   + "Mauris laoreet tincidunt tellus vel faucibus."

   isDisabled:boolean = false;

   onClick(){
    this.isDisabled = true;
    setTimeout(()=>{
      this.message = "I am changed after 10 seconds on button click."
    }, 10000);
   }
}
app.component.html

<h2 style="padding-top:25px; padding-bottom:25px;">
    Angular - How to use setTimeout()
</h2>

<button 
    (click)="onClick()"
    [disabled]="isDisabled"
    style="margin-bottom:20px; padding:12px;">
        Change Text (delay 10 seconds)
</button>

<h3 style="color:blue;">
    {{message}}
</h3>
More Angular tutorials