]>
Commit | Line | Data |
---|---|---|
c16ce1de | 1 | import { setInterval } from 'timers' |
eb4f957e C |
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(); | |
872a4c7c | 20 | this.runInterval(); |
eb4f957e C |
21 | } |
22 | ||
23 | ngOnDestroy() { | |
feb4bdfd | 24 | if (this.stats !== null && this.stats.secondsInterval !== null) { |
eb4f957e C |
25 | clearInterval(this.interval); |
26 | } | |
27 | } | |
28 | ||
eb4f957e C |
29 | getStats() { |
30 | this.requestService.getStats().subscribe( | |
872a4c7c | 31 | stats => this.stats = stats, |
eb4f957e | 32 | |
da4971c1 | 33 | err => alert(err.text) |
eb4f957e C |
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); | |
eb4f957e C |
43 | } |
44 | }, 1000); | |
45 | } | |
46 | ||
47 | ||
48 | } |