Angular 6 run a function in every X seconds

后端 未结 5 1369
我寻月下人不归
我寻月下人不归 2021-01-03 21:53

I have a function called

opensnack(text) { ... };

which is opening an angular material snackbar with the given text input.

What I

5条回答
  •  执念已碎
    2021-01-03 22:21

    Angular 6 run a function in every X seconds

    import { Component, OnInit } from '@angular/core';
    import { Observable } from 'rxjs';
    
    @Component({
        selector: 'my-app',
        template: `

    Now {{time | date: 'hh:mm:ss'}}
    Now {{time$ | async | date: 'hh:mm:ss'}}

    ` }) export class AppComponent { time: Date; time$; constructor() { // First way: manual subscribe Observable .interval(1000) .map(() => new Date()) .subscribe(res => this.time = res) // Angular way: by using the `async` pipe this.time$ = Observable.interval(1000).map(() => new Date()) } }

提交回复
热议问题