-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
114 lines (100 loc) · 2.65 KB
/
script.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const introDiv = document.querySelector('.intro');
const quotesDiv = document.querySelector('.quotes');
const controlsDiv = document.querySelector('.controls');
const loaderDiv = document.querySelector('.loader');
const oneQuoteButton = document.getElementById('oneQuote');
const fiveQuotesButton = document.getElementById('fiveQuotes');
const oneMoreButton = document.getElementById('oneMore');
const fiveMoreButton = document.getElementById('fiveMore');
const API_URL = 'https://corporatebs-generator.sameerkumar.website/';
oneQuoteButton.addEventListener('click', () => {
fadeOut(introDiv);
showLoader();
setTimeout(() => {
hideLoader();
getQuotes(1);
showControls();
}, 1000);
});
fiveQuotesButton.addEventListener('click', async () => {
fadeOut(introDiv);
showLoader();
const quotes = [];
for (let i = 0; i < 5; i++) {
await new Promise(resolve => setTimeout(resolve, 1000));
const quote = await fetchQuote();
quotes.push(quote);
}
hideLoader();
displayQuotes(quotes);
showControls();
});
oneMoreButton.addEventListener('click', () => {
clearQuotes();
showLoader();
setTimeout(() => {
hideLoader();
getQuotes(1);
showControls();
}, 1000);
});
fiveMoreButton.addEventListener('click', async () => {
clearQuotes();
showLoader();
const quotes = [];
for (let i = 0; i < 5; i++) {
await new Promise(resolve => setTimeout(resolve, 1000));
const quote = await fetchQuote();
quotes.push(quote);
}
hideLoader();
displayQuotes(quotes);
showControls();
});
async function fetchQuote() {
const response = await fetch(API_URL);
const data = await response.json();
return data.phrase;
}
function getQuotes(count) {
Promise.all([...Array(count)].map(() => fetchQuote()))
.then(quotes => {
displayQuotes(quotes);
showControls();
})
.catch(error => {
console.error('Error fetching quotes:', error);
});
}
function displayQuotes(quotes) {
quotesDiv.innerHTML = '';
quotes.forEach(quote => {
const quoteBox = document.createElement('div');
quoteBox.classList.add('quote-box');
quoteBox.textContent = quote;
quotesDiv.appendChild(quoteBox);
});
}
function clearQuotes() {
quotesDiv.innerHTML = '';
controlsDiv.style.display = 'none';
}
function fadeOut(element) {
element.style.opacity = 1;
(function fade() {
if ((element.style.opacity -= 0.1) < 0) {
element.style.display = 'none';
} else {
requestAnimationFrame(fade);
}
})();
}
function showLoader() {
loaderDiv.style.display = 'block';
}
function hideLoader() {
loaderDiv.style.display = 'none';
}
function showControls() {
controlsDiv.style.display = 'flex';
}