-
Notifications
You must be signed in to change notification settings - Fork 2
/
joke.js
37 lines (35 loc) · 1.12 KB
/
joke.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
function ajaxGet(url, successCallBack, errorCallback) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
if (xmlhttp.status == 200) {
successCallBack(xmlhttp.responseText);
} else if (errorCallback) {
errorCallback(xmlhttp);
}
}
};
xmlhttp.open('GET', url);
xmlhttp.send(null);
}
function showJoke(joke = 'Raise your standards to get a Chuck Norris fact!') {
document.getElementById('loading').style.display = 'none';
document.getElementById('content').style.display = 'block';
document.getElementById('joke').innerText = joke;
}
ajaxGet('https://api.icndb.com/jokes/random?limitTo=[nerdy]',
function (res) {
try {
res = JSON.parse(res);
if (res.type == 'success' && res.value && res.value.joke) {
showJoke(res.value.joke);
} else {
showJoke();
}
} catch (err) {
showJoke();
}
},
function (err) {
showJoke();
});