]>
Commit | Line | Data |
---|---|---|
1 | import { setInterval } from 'timers' | |
2 | import { Component, OnInit, OnDestroy } from '@angular/core'; | |
3 | ||
4 | import { RequestService, RequestStats } from '../shared'; | |
5 | ||
6 | @Component({ | |
7 | selector: 'my-request-stats', | |
8 | templateUrl: './request-stats.component.html', | |
9 | styleUrls: [ './request-stats.component.scss' ] | |
10 | }) | |
11 | export class RequestStatsComponent implements OnInit, OnDestroy { | |
12 | stats: RequestStats = null; | |
13 | ||
14 | private interval: NodeJS.Timer = null; | |
15 | ||
16 | constructor(private requestService: RequestService) { } | |
17 | ||
18 | ngOnInit() { | |
19 | this.getStats(); | |
20 | this.runInterval(); | |
21 | } | |
22 | ||
23 | ngOnDestroy() { | |
24 | if (this.stats !== null && this.stats.secondsInterval !== null) { | |
25 | clearInterval(this.interval); | |
26 | } | |
27 | } | |
28 | ||
29 | getStats() { | |
30 | this.requestService.getStats().subscribe( | |
31 | stats => this.stats = stats, | |
32 | ||
33 | err => alert(err.text) | |
34 | ); | |
35 | } | |
36 | ||
37 | private runInterval() { | |
38 | this.interval = setInterval(() => { | |
39 | this.stats.remainingMilliSeconds -= 1000; | |
40 | ||
41 | if (this.stats.remainingMilliSeconds <= 0) { | |
42 | setTimeout(() => this.getStats(), this.stats.remainingMilliSeconds + 100); | |
43 | } | |
44 | }, 1000); | |
45 | } | |
46 | ||
47 | ||
48 | } |