How do I call a function in every 10 seconds in Angular 2?

Issue

How do I call a function in a set Time interval in Angular 2. I want it to be called/Triggered at specific time intervals(for eg 10 secs).
For Eg:
ts File

num: number = 0;
array: number[] = [1,5,2,4,7];
callFuntionAtIntervals(){
    if(num==5){
        num=0;    
    }
    num++;
}

HTML:

<div>{{ array[num] }}</div>

So basically the div value will change at intervals

Solution

Observable.interval(10000).takeWhile(() => true).subscribe(() => this.function());

infinite loop where every each 10 seconds function() is being called

Answered By – elzoy

Answer Checked By – Mildred Charles (AngularFixing Admin)

Leave a Reply

Your email address will not be published.