-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
39 lines (35 loc) · 856 Bytes
/
app.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
const buttonEl = document.getElementById('button');
const audioEl = document.getElementById('audio');
function toggleButton() {
buttonEl.disabled = !buttonEl.disabled;
}
function tellJoke(joke) {
VoiceRSS.speech({
key: 'Paste a voiceRSS API key here',
src: joke,
hl: 'es-es',
r: 0,
c: 'mp3',
f: '48khz_16bit_stereo',
ssml: false,
});
}
async function getJoke() {
let joke = '';
const apiUrl = 'https://v2.jokeapi.dev/joke/Any?lang=es';
try {
const response = await fetch(apiUrl);
const data = await response.json();
if (data.setup) {
joke = `${data.setup} ... ${data.delivery}`;
} else {
joke = data.joke;
}
tellJoke(joke);
toggleButton();
} catch (error) {
return;
}
}
buttonEl.addEventListener('click', getJoke);
audioEl.addEventListener('ended', toggleButton);