-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
28 lines (23 loc) · 712 Bytes
/
Copy pathscript.js
File metadata and controls
28 lines (23 loc) · 712 Bytes
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
let textElement = document.getElementById("adviceContent");
onButtonClick();
async function onButtonClick() {
try {
await fetchAnotherAdvice();
} catch (error) {
console.error(error);
}
}
async function fetchAnotherAdvice() {
const emptyString = " ";
let advice = emptyString;
textElement.innerHTML = "Loading...";
await fetch("https://api.adviceslip.com/advice")
.then((resp) => resp.json())
.then((data) => (advice = data.slip.advice));
textElement.innerHTML = emptyString;
const words = advice.split(emptyString);
for (let i = 0; i < words.length; i++) {
textElement.innerHTML += ` ${words[i]}`;
await new Promise((resolve) => setTimeout(resolve, 200));
}
}