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

前端 未结 4 1987
没有蜡笔的小新
没有蜡笔的小新 2021-01-14 11:23

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

nu         


        
4条回答
  •  迷失自我
    2021-01-14 12:00

    In your TS logic, define an observable based on an "interval", which will emit the values 0, 1, 2, 3, 4, 0, 1, ...

    this.index = Observable.interval(10000).map(n => n % this.array.length);
    

    In your component, unwrap that observable using async and use it to index into the array.

    {{array[index | async]}}
    

提交回复
热议问题