-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
70 lines (61 loc) · 2.24 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { mapFlight } from './mapFlight.js';
import { getDistance } from './getDistance.js';
const tbody = document.getElementById('tbody');
const render = (flights = []) => {
flights.forEach((item, index) => tbody.insertAdjacentHTML('beforeend',
`<tr>
<th scope='row'>${index}</th>
<td>Широта: ${item.latitude}\nДолгота: ${item.longitude}</td>
<td>${item.speed}</td>
<td>${item.course}</td>
<td>${+(item.altitude / 3.281).toFixed(2)}</td>
<td>${item.departureAirport}</td>
<td>${item.destinationAiroport}</td>
<td>${item.flightNumber}</td>
<td>${item.distanceToDemodedovo}</td>
</tr>`
));
};
const getMapFlight = (data) => {
const flights = [];
for (let key in data) {
if (Array.isArray(data[key])) {
flights.push(mapFlight(data[key]));
}
}
return flights;
};
async function loadFlights() {
try {
const r = await fetch('https://data-live.flightradar24.com/zones/fcgi/feed.js?bounds=56.84,55.27,33.48,41.48');
if (r.status !== 200) {
console.log('Status code: ' + r.status);
return;
}
return r.json();
}
catch (err) {
return console.log('Fetch Error:' + err);
}
}
const clearElem = (elem) => {
elem.innerHTML = '';
};
const getFlights = () => {
return loadFlights()
.then(r => getMapFlight(r)) //Создаем мапу для полученных данных
.catch(error => new Error(error))
.then(r => r.map(item => getDistance(item))) //Счиатем дистанцию до Домодедово
.catch(error => new Error(error))
.then(r => r.sort((a, b) => a.distanceToDemodedovo - b.distanceToDemodedovo))
.catch(error => new Error(error))
.then(clearElem(tbody)) //Поставил очистку перед отрисовкой,
// т.к. при медленном интернете возникает долгая пауза.
//Буду рад услышать, как это сделать лучше.
.then(r => render(r)) // Отрисовка
.catch(error => new Error(error));
};
getFlights();
setInterval(() => { //Загружаем данные и обновляем таблицу каждые 5 сек
getFlights();
}, 5000);