-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
99 lines (92 loc) · 2.8 KB
/
index.html
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Next Train Arrival Times</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f8f9fa;
}
h1 {
text-align: center;
color: #333;
}
.train-schedule {
max-width: 600px;
margin: auto;
border: 1px solid #ddd;
border-radius: 5px;
padding: 15px;
background-color: #fff;
}
.train {
border-bottom: 1px solid #ddd;
padding: 10px 0;
}
.train:last-child {
border-bottom: none;
}
.train-info {
display: flex;
justify-content: space-between;
}
.time {
font-weight: bold;
color: #28a745;
}
.destination {
color: #007bff;
}
.station-name {
text-align: center;
font-size: 1.2em;
margin-bottom: 10px;
color: #333;
}
</style>
</head>
<body>
<h1>Next Train Arrival Times</h1>
<div class="train-schedule" id="train-schedule">
<div class="station-name" id="station-name">Station Name: CF</div>
<!-- Train information will be populated here -->
</div>
<script>
async function fetchTrainSchedule() {
const response = await fetch('https://rt.data.gov.hk/v1/transport/mtr/lrt/getSchedule?station_id=468');
const data = await response.json();
const scheduleDiv = document.getElementById('train-schedule');
scheduleDiv.innerHTML = '<div class="station-name" id="station-name">Station Name: CF</div>'; // Reset station name
if (data.status === 1) {
const currentTime = new Date();
data.platform_list.forEach(platform => {
platform.route_list.forEach(route => {
const trainDiv = document.createElement('div');
trainDiv.className = 'train';
// Calculate arrival time
const arrivalTime = new Date(currentTime.getTime() + parseInt(route.time_en) * 60 * 1000);
const options = { hour: 'numeric', minute: 'numeric', hour12: true };
const formattedArrivalTime = arrivalTime.toLocaleString('en-US', options);
trainDiv.innerHTML = `
<div class="train-info">
<div class="destination">${route.route_no} ${route.dest_ch}</div>
<div class="time">${route.time_ch} ${formattedArrivalTime}</div>
</div>
`;
scheduleDiv.appendChild(trainDiv);
});
});
} else {
scheduleDiv.innerHTML = '<p>Unable to fetch train schedule.</p>';
}
}
// Fetch the train schedule immediately and then every 2 seconds
fetchTrainSchedule();
setInterval(fetchTrainSchedule, 1000); // 2000 ms = 2 seconds
</script>
</body>
</html>