Skip to content

Commit 9cdb7d7

Browse files
committed
Programmer humour clean submission
1 parent fe7124b commit 9cdb7d7

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

fetch/programmer-humour/script.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
function fetchComic() {
22
const container = document.getElementById("comic-container");
3+
const loading = document.getElementById("loading");
34

45
fetch("https://xkcd.now.sh/?comic=latest")
56
.then(response => {
@@ -11,14 +12,21 @@ function fetchComic() {
1112
.then(data => {
1213
console.log(data); // requirement
1314

15+
// remove loading message
16+
if (loading) loading.remove();
17+
1418
container.innerHTML = `
1519
<h2>${data.title}</h2>
1620
<img src="${data.img}" alt="${data.alt}" />
1721
<p>${data.alt}</p>
1822
`;
1923
})
2024
.catch(error => {
21-
container.innerHTML = `<p>Error loading comic 😢</p>`;
25+
if (loading) {
26+
loading.textContent = "Error loading comic 😢";
27+
} else {
28+
container.innerHTML = `<p>Error loading comic 😢</p>`;
29+
}
2230
console.error(error);
2331
});
2432
}

0 commit comments

Comments
 (0)