Repository navigation
Expand file tree
/
Copy pathreview.js
More file actions
183 lines (135 loc) · 5.41 KB
/
Copy pathreview.js
File metadata and controls
183 lines (135 loc) · 5.41 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
Lets quickly reiterate the call stack
*/
let hasBoughtGroceries = false;
let hasWatchedMovie = false;
function buyGroceries() {
hasBoughtGroceries = true;
}
function watchmovie() {
hasBoughtGroceries = true;
}
buyGroceries();
watchmovie();
// Lets start today off with a function that takes a function as input
// The function is just a variable
const nameLogger = function (number) {
console.log('Names names and lots and lots of NAMES!!!');
}
const cityLogger = function (number) {
console.log('Copenhagen, Silkeborg, Ry');
}
function logger(logFunction) {
logFunction();
}
logger(cityLogger);
/*
Events
Events in javascript are thing like:
A timer has just finished, a user clicked a button, our page has loaded,
someone types into an input element or we have just gotten some data from a server.
When these events happen, we usually want to add some functionality.
Fx when a user clicks the like button (event), we want to increment the like counter and color the like button blue.
Or when someone clicks "Close cookies" (event) we want to remove the cookie div.
Lets first try to create some js that waits for 2 seconds and the console.logs out "2 seconds has elapsed!"
In javascript we use the word eventlistener to listen
*/
setTimeout(function() {
console.log("2 seconds has elapsed!");
}, 2000);
// Cool, now lets make a function as a variable:
const fourSecondLog = function() {
console.log("4 seconds has elapsed!");
}
setTimeout(fourSecondLog, 4000);
// Now lets try and log out "button clicked!" when a button is clicked.
// To check if a button gets clicked we use a what is called an eventlistener.
// Imagine a person listening to the click of a button and everytime he hears a click he yells out "CLICKED".
const buttonElement = document.querySelector('button');
buttonElement.addEventListener('click', function() {
console.log("Button clicked!");
});
const buttonClicked = function(){
console.log("Button clicked as a variable!");
}
// Cool man! Lets try and add that function as a variable.
buttonElement.addEventListener('click', buttonClicked);
//Callbacks
// Now lets learn about callbacks!
// Well actually you have already made callbacks!
// When you give a function to an event listener or a timer or when fetching data you are using a callback function
// Lets create a callback function when someone writes in a input element
const callback = function() {
console.log("Someone is writing!!");
}
document.querySelector('input').addEventListener('input', callback);
// Ajax Requests
// When us frontend developers want to get information about a logged in user,
// get search results from a search, check if someones passwors is correct
// we have to ask a server form these things. This communication between a client and
// a server happens with using ajax requests.
//
const url = 'https://api.github.com/users/benna100';
// Create new ajax call with the js function called XMLHttpRequest
const req = new XMLHttpRequest();
req.addEventListener('load', function () {
// This in here is our callback function
// Check our server responsecode, 200 means ok, success: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
if (this.status === 200) {
const user = JSON.parse(req.responseText);
console.log(user);
} else {
console.log('Something is probably wrong with the url');
}
});
req.addEventListener('error', function () {
console.log('Server error like timeout');
});
// initializes a request with an http method
req.open("GET", url);
// Sends the request
req.send();
// Show the network tab in chrome and filtering for xhr
/*
API Application programming interface
What is an interface
Programmers can use to an api to access complex functionality in a simple way.
Electricity socket. You just plug your appliance into the wall and it works.
We dont have to worry about the wiring or anything. The complex functionality has been abstracted away.
Example of apis:
- DOM document.queryselector,
- Geolocation:navigator.geolocation.getCurrentPosition
- accelerometer,
- facebook
- twitter
Web apis are like that, but just for getting data from a server.
There are some funny apis to play with:
http://deckofcardsapi.com/
http://api.giphy.com/v1/gifs/search?api_key=2FuF3E9nyFeXWt3aHIfkxtKTUGn73v0w&q=smile
https://github.com/toddmotto/public-apis
This incredibly unique service generates true random numbers by measuring quantum fluctuations of a vacuum in real-time!
https://qrng.anu.edu.au/API/jsonI.php?length=1&type=uint8’
https://yesno.wtf/api/
*/
const api_key = '2FuF3E9nyFeXWt3aHIfkxtKTUGn73v0w';
const query = 'smile'
const giphyUrl = 'http://api.giphy.com/v1/gifs/search?api_key=' + api_key + '&q=' + query;
// Create new ajax call with the js function called XMLHttpRequest
const giphyReq = new XMLHttpRequest();
giphyReq.addEventListener('load', function () {
// This in here is our callback function
// Check our server responsecode, 200 means ok, success: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
if (this.status === 200) {
const gifs = JSON.parse(giphyReq.responseText);
console.log(gifs);
} else {
console.log('Something is probably wrong with the url');
}
});
giphyReq.addEventListener('error', function () {
console.log('Server error like timeout');
});
// initializes a request with an http method
giphyReq.open("GET", giphyUrl);
// Sends the request
giphyReq.send();