-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
43 lines (38 loc) · 1.08 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
<!DOCTYPE html>
<html>
<head>
<title>Spy / Stub / Clock</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<h1>Favorite Fruits</h1>
<ul class="favorite-fruits"></ul>
<script>
function updateFavoriteFruits (contents) {
if (typeof contents !== 'string') {
contents = contents.map((item) => `<li>${item}</li>`).join('')
}
document.querySelector('.favorite-fruits').innerHTML = contents
}
function getFavoriteFruits () {
document.querySelector('.favorite-fruits').innerHTML = '<div class="loader"></div>'
fetch('/favorite-fruits')
.then((response) => {
if (response.ok) {
return response.json()
} else {
throw new Error(response.statusText)
}
})
.then((response) => {
updateFavoriteFruits(response.length ? response : 'No favorites')
})
.catch((error) => {
updateFavoriteFruits(`Failed loading favorite fruits: ${error.message}`)
})
}
getFavoriteFruits()
setInterval(getFavoriteFruits, 30000)
</script>
</body>
</html>