Try with timer
from RxJS :
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subscription, timer } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { MyService } from 'path/to/the/service/my-service.service';
@Component({
......
})
export class MyExampleComponent implements OnInit, OnDestroy {
subscription: Subscription;
statusText: string;
constructor(private myService: MyService) {}
ngOnInit() {
this.subscription = timer(0, 10000).pipe(
switchMap(() => this.myService.checkdata())
).subscribe(result => this.statusText = result);
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
interval(10000)
from RxJS is not appropriate, because it will start to emit values ONLY after 10sec and not immediatly for the first time (and I think that's not what you're looking for).
However, timer(0, 10000)
, will emit values immediatly (0) and every 10sec (10000) until unsubscription.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…