-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScript.js
158 lines (107 loc) · 3.77 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const oldDiv = document.querySelector(".loading-state");
const btnJoke = document.querySelector(".joke-generator");
function generateJoke (){
const xhr = new XMLHttpRequest();
xhr.open("GET","https://api.chucknorris.io/jokes/random");
xhr.onreadystatechange = function(){
if(this.readyState === 4 && this.status === 200){
oldDiv.innerHTML = JSON.parse(this.responseText).value;
oldDiv.style.fontSize = "20px";
}
else{
oldDiv.innerHTML = "Something went wrong try again";
}
};
xhr.send();
}
btnJoke.addEventListener("click",generateJoke);
document.addEventListener("DOMContentLoaded",generateJoke);
/*const xhr = new XMLHttpRequest();
//xhr.open("GET","./movie.json");
xhr.open("GET","https://api.github.com/users/bradtraversy/repos");
// readyState has 5 possible values
// - 0 request not initialised
// - 1 server connection established
// - 2 request recievd
// - 3 processing request
// - 4 request finished and response is ready
// From an api(AJAX)
xhr.onreadystatechange = function(){
if(this.readyState === 4 && this.status === 200){
//console.log(JSON.parse(this.responseText));
const data = JSON.parse(this.responseText);
data.forEach(repo =>{
const li = document.createElement("li");
li.innerHTML = `<strong>${repo.name}</strong> - ${repo.description}`;
document.querySelector("#results").appendChild(li);
})
}
}
xhr.send();
//From a file(AJAX)
// xhr.onreadystatechange = function(){
// if(this.readyState === 4 && this.status === 200){
// //console.log(JSON.parse(this.responseText));
// const data = JSON.parse(this.responseText);
// data.forEach(movie =>{
// const li = document.createElement("li");
// li.innerHTML = `<strong>${movie.title}</strong> - ${movie.year}`;
// document.querySelector("#results").appendChild(li);
// })
// }
// }
// xhr.send();
//function toggle(e){
// e.target.classList.toggle("danger");
//}
//document.querySelector("button").addEventListener("click",toggle);
// const posts = [{title: "Post One",body:"This is post one"},
// {title: "Post Two",body:"This is post Two"}
// ];
// function createPost(post,cb){
// setTimeout(()=>{
// posts.push(post);
// cb();
// },2000)
// }
// function getPosts(){
// setTimeout(()=>{
// posts.forEach(function(post){
// const div = document.createElement("div");
// div.innerHTML = `<strong>${post.title}</strong>-${post.body}`;
// document.querySelector("#posts").appendChild(div);
// })
// },1000);
// }
// createPost({title:"Post Three",body:"This is post Three"},getPosts);
//setTimeout(changeText(),2000);
/*function changeText(){
document.querySelector("h1").textContent = "Hello from Callback";
}
const timerId = setTimeout(changeText,3000);
document.querySelector("#cancel").addEventListener("click",()=>{
console.log(timerId);
clearTimeout(timerId);
console.log("Timer Cancelled");
});*/
/*let intervalID;
function startChange(){
if(!intervalID){
intervalID = setInterval(changeColor,1000);
}
}
function changeColor(){
if(document.body.style.backgroundColor !== "black"){
document.body.style.backgroundColor = "black";
document.body.style.color = "white";
}
else{
document.body.style.backgroundColor = "white";
document.body.style.color = "black";
}
}
function stopChange(){
clearInterval(intervalID);
}
document.getElementById("Start").addEventListener("click",startChange);
document.getElementById("Stop").addEventListener("click",stopChange);*/