-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
31 lines (28 loc) 路 841 Bytes
/
Copy pathindex.js
File metadata and controls
31 lines (28 loc) 路 841 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
29
30
31
const idContainer = document.getElementById("id-container");
const adviceContainer = document.getElementById("advice-container");
const nextAdviceBtn = document.getElementById("next-advice");
let id = 1;
function sendHttpRequest(method, url) {
const promise = new Promise((resolve, reject) => {
const request = new XMLHttpRequest();
request.open(method, url);
request.onload = function () {
resolve(JSON.parse(this.response).slip);
};
request.send();
});
return promise;
}
function displayAdvice() {
sendHttpRequest("GET", `https://api.adviceslip.com/advice/${id}`).then(
(response) => {
idContainer.innerHTML = `#${response.id}`;
adviceContainer.innerHTML = response.advice;
}
);
id += 1;
}
displayAdvice();
nextAdviceBtn.addEventListener("click", () => {
displayAdvice();
});