-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (47 loc) · 1.74 KB
/
index.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
43
44
45
46
47
48
49
50
51
52
53
54
let domain = "https://agumballofkindness.vercel.app:3000";
document
.getElementById("random-gumball-button")
.addEventListener("click", () => {
document.getElementById("random-gumball-button").disabled = true;
setTimeout(() => {
document.getElementById("random-gumball-button").disabled = false;
}, 3000);
fetch(`${domain}/random-gumball`)
.then((response) => response.json())
.then((data) => {
console.log(data);
document.getElementById("gumball").innerText = `${data.message}`;
document.getElementById("gumball").style.backgroundColor = data.color;
})
.then(() => {
document.getElementById(
"random-gumball-button"
).textContent = `Here's a gumball! ✅`;
document.getElementById("random-gumball-button").style.backgroundColor =
"green";
setTimeout(() => {
document.getElementById("random-gumball-button").textContent =
"Get Random Gumball";
document.getElementById(
"random-gumball-button"
).style.backgroundColor = "#ff6f61";
}, 3000);
})
.catch((error) => {
console.error("Error:", error);
});
});
document
.getElementById("random-gumball-button")
.addEventListener("click", () => {
const gumballElement = document.getElementById("gumball");
gumballElement.classList.remove("dispense");
void gumballElement.offsetWidth;
gumballElement.classList.add("dispense");
const messages = ["😊", "🍬", "🎉", "💖", "🌟"];
const randomMessage =
messages[Math.floor(Math.random() * messages.length)];
setTimeout(() => {
gumballElement.textContent = randomMessage;
}, 2000);
});