forked from thinkswell/javascript-mini-projects
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
73 lines (51 loc) · 1.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
const quoteText = document.getElementById('quote-text');
const author = document.getElementById('author');
const newQuoteBtn = document.getElementById('new-quote-btn');
const themeSelect = document.getElementById('theme-select');
let quoteChangeInterval;
async function getRandomQuote(theme) {
try {
const response = await fetch(`https://api.quotable.io/random?theme=${theme}`);
const data = await response.json();
return data;
} catch (error) {
console.error('Error fetching quote:', error);
return null;
}
}
function getRandomColor() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
async function displayNewQuote() {
const selectedTheme = themeSelect.value;
const newQuote = await getRandomQuote(selectedTheme);
if (newQuote) {
quoteText.textContent = newQuote.content;
author.textContent = `- ${newQuote.author}`;
document.body.style.backgroundColor = getRandomColor();
}
}
function startAutoChangeInterval() {
quoteChangeInterval = setInterval(displayNewQuote, 300000);
}
function resetAutoChangeInterval() {
clearInterval(quoteChangeInterval);
startAutoChangeInterval();
}
newQuoteBtn.addEventListener('click', () => {
displayNewQuote();
resetAutoChangeInterval();
});
displayNewQuote();
startAutoChangeInterval();
themeSelect.addEventListener('change', () => {
displayNewQuote();
resetAutoChangeInterval();
});
document.addEventListener('keydown', resetAutoChangeInterval);
startAutoChangeInterval();