-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
77 lines (65 loc) · 1.69 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
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment-with-locales.js"></script>
<link href='http://fonts.googleapis.com/css?family=Raleway:100' rel='stylesheet' type='text/css'>
<style type="text/css">
h1,h3,p {
font-family: 'Raleway', sans-serif;
font-weight: 100;
}
</style>
</head>
<body>
<div class="bus"></div>
</body>
<script type="text/javascript">
// set language
moment.locale('nb');
// request
function httpGet(url) {
console.log("httpGet()");
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", url, false );
xmlHttp.send( null );
return xmlHttp.responseText;
}
// get time to bus
function setTimes(){
console.log("setTimes()");
var object = httpGet('http://bybussen.api.tmn.io/rt/16011394');
var nextBus = moment(JSON.parse(object).next[0].t, "DD.MM.YYYY HH:mm");
return nextBus
}
// variables
var next = setTimes();
var timeToBus = moment(next).fromNow();
var timeOfBus = moment(next).format(" (HH:mm)");
update();
// update/add to html
function update(){
console.log("update()");
var divBus = document.querySelector('.bus');
if(divBus.hasChildNodes()){
divBus.removeChild(divBus.lastChild);
}
var time = document.createElement('h1');
time.textContent = timeToBus + timeOfBus;
divBus.appendChild(time);
}
// check times
setInterval(function calculate(){
console.log("calculate()");
if(timeToBus != moment(next).fromNow()){
timeToBus = moment(next).fromNow();
update();
}
if(moment().isAfter(next)){
// request new bus times
next = setTimes();
timeOfBus = moment(next).format(" (HH:mm)");
update();
}
}, 3000);
</script>
</html>