-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
42 lines (39 loc) · 1.21 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
const adviceIdSpan = document.querySelector(".advice-id");
const quoteDiv = document.querySelector(".quote");
const ctaBtn = document.querySelector(".cta");
const dualRing = document.querySelector(".lds-dual-ring");
const title = document.querySelector(".title");
let isDisabled = false;
document.addEventListener(
"DOMContentLoaded",
async () => {
await fetchAdvice();
},
false
);
ctaBtn.addEventListener("click", async () => {
if (isDisabled)
return;
adviceIdSpan.innerHTML = '';
quoteDiv.innerHTML = '';
await fetchAdvice();
});
const fetchAdvice = async () => {
title.style.marginBottom = "6rem";
dualRing.classList.remove("disabled");
ctaBtn.classList.add("disabled");
isDisabled = true;
setTimeout(async () => {
const response = await fetch("https://api.adviceslip.com/advice");
const data = await response.json();
const { id, advice } = data.slip;
adviceIdSpan.innerHTML = id;
quoteDiv.innerHTML = `“${advice}”`;
dualRing.classList.add("disabled");
title.style.marginBottom = "0";
}, 500);
setTimeout(() => {
ctaBtn.classList.remove("disabled");
isDisabled = false;
}, 2000);
};