How to Use SetInterval in Angular 13 ?

Today now in this post i will show you How to Use SetInterval in Angular 13?Here we know about angular 13 setinterval topic. It’s so simple example of setinterval angular 13 function. Here we’ll learn about how to use setinterval in angular 13. Now in this tutorial i will give you a simple example of how to use setinterval and clearinterval in angular 13. So here, Creating a basic example of angular 13 clearinterval example.

Here, I will give you a very simple example of how we can use setinterval and clearinterval in angular applications. So here i will create one callMethod() and also call it every 5 seconds with a console log message. So it will be clear on ngOnDestroy() function.

Let’s start and see the simple ts file code and output.

src/app/app.component.ts
import { Component, OnInit, OnDestroy } from '@angular/core';
    
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, OnDestroy{
  title = 'app-material2';
  id = 0;
    
  /*------------------------------------------
  --------------------------------------------
  Ng On Init
  --------------------------------------------
  --------------------------------------------*/
  ngOnInit() {
    this.callMethod();
    this.id = setInterval(() => {
      this.callMethod(); 
    }, 5000);
  }
  
  /*------------------------------------------
  --------------------------------------------
  ngOnDestroy
  --------------------------------------------
  --------------------------------------------*/
  ngOnDestroy() {
    if (this.id) {
      clearInterval(this.id);
    }
  }
  
  /*------------------------------------------
  --------------------------------------------
  callMethod
  --------------------------------------------
  --------------------------------------------*/
  callMethod(){
    console.log('Call Function Every Five Seconds.', new Date());
  }
  
}

Read Also: How To Implement Laravel 9 form validation ?

Thanks for read. I hope it help you. For more you can follow us on facebook

close

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →