-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
52 lines (41 loc) · 1.11 KB
/
app.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
const adviceBtn = document.getElementById("adviceBtn");
const adviceNum = document.getElementById("adviceNum");
const adviceText = document.getElementById("adviceText");
const card = document.getElementById("card");
const endpoint = "https://api.adviceslip.com/advice";
let clicked = false;
function getAdvice() {
startLoading();
fetch(endpoint)
.then((response) => response.json())
.then((advice) => {
adviceNum.innerText = advice.slip.id;
adviceText.innerText = advice.slip.advice;
})
.catch((err) => console.log(err))
.finally(() => finishLoading());
}
function startLoading() {
card.style.opacity = 0;
}
function finishLoading() {
card.style.opacity = 1;
}
function needAdvice() {
if (clicked) {
startLoading();
setTimeout(() => {
adviceNum.innerText = "0";
adviceText.innerText = "Did you actually read the previous advice?";
finishLoading();
}, 500);
} else {
clicked = true;
getAdvice();
setTimeout(() => {
clicked = false;
}, 5000);
}
}
window.onload = () => getAdvice();
adviceBtn.addEventListener("click", needAdvice);