카운트 다운이 완료되면 API 데이터를 업데이트하는 타이머를 설정하고 API 데이터가 업데이트되면 원래의 카운트 다운으로 되돌아갑니다.각도 4 | 타이머를 설정하고 API 데이터를 업데이트하려면 어떻게해야합니까?
예 10까지 카운트하고 싶으면 API 데이터가 업데이트됩니다.
다음은 타이머가있는 코드입니다.
import { Component, ViewEncapsulation, Pipe, PipeTransform, VERSION } from '@angular/core';
import { Http, Response, RequestOptions, Headers } from '@angular/http';
import { SlicePipe, DatePipe } from '@angular/common';
import {Observable} from 'rxjs/Observable'
import 'rxjs/add/observable/timer'
import 'rxjs/add/operator/map'
import 'rxjs/add/operator/take'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
encapsulation: ViewEncapsulation.None,
})
export class AppComponent{
public data: any;
countDown;
counter = 10;
constructor(private http: Http) {
this.countDown = Observable.timer(0,1000)
.take(this.counter)
.map(() => --this.counter);
}
ngOnInit() {
this.getVoicepicData();
this.getData();
console.log('hi');
}
getData(){
let headers = new Headers({
'token': "MY TOKEN GOES HERE",
'Content-Type': 'application/json'
});
let options: RequestOptions = new RequestOptions({ headers: headers });
return this.http.get('http://my.apidatahere.com/api/', options)
.map((res: Response) => res.json())
}
getVoicepicData() {
this.getData().subscribe(data => {
this.data = data
})
}
}
현재 코드의 문제점은 무엇입니까? –
theres 현재 코드와 아무런 문제가 있지만 카운트 다운 0 때 API 데이터를 업데이트 할 때 다시 시작될 때 API 데이터를 업데이트하려면 내 타이머 메서드를 구현하는 방법을 모릅니다. –