-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
43 lines (35 loc) · 1.02 KB
/
app.js
File metadata and controls
43 lines (35 loc) · 1.02 KB
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
let button = document.querySelector(".generate-btn");
let text = document.querySelector(".text");
let coolDown = false;
button.addEventListener("click", () => {
if (!coolDown) {
getJoke().then(joke => {
text.textContent = joke;
});
coolDown = true;
setTimeout(() => {
coolDown = false;
}, 2000);
} else {
text.textContent = "Sorry! Wait for abit before getting a new joke."
}
});
let getJoke = async () => {
try {
let apiResponse = await fetch("https://icanhazdadjoke.com", {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
});
if (!apiResponse.ok) {
return "Trouble getting joke! Try again please";
}
let data = await apiResponse.json();
let newJoke = data.joke;
return newJoke;
} catch (error) {
console.error(`Error has occured it reads ${error}`)
}
}