-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimer.js
More file actions
236 lines (190 loc) · 4.34 KB
/
timer.js
File metadata and controls
236 lines (190 loc) · 4.34 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 1);
}
// In what order will the numbers 1-4 be logged to the console when the code below is executed? Why?
(function() {
console.log(1);
setTimeout(function(){console.log(2)}, 1000);
setTimeout(function(){console.log(3)}, 0);
console.log(4);
})();
function doAsyncTask(cb) {
setTimeout(() => {
console.log("Async Task Calling Callback");
cb();
},
1000);
}
doAsyncTask(() => console.log("Callback Called"));
// o/p-
// Async Task Calling Callback
// Callback Called
// What is logged?
// it logs 5 five times since settimeout runs the log function after for loop is done.
// settimeout is an async process.
let i;
for (i = 0; i < 5; i++) {
const log = () => {
console.log(i); // 5 5 times
};
setTimeout(log, 100);
}
for (var g = 0; g < 5; g++) {
setTimeout(function () {
console.log(g, 'g');
}, 100);
}
// 5
// 5
// 5
// 5
// 5
// we can resolve this problem by passing i in the log method.
let j;
for (j = 0; j < 5; j++) {
const log = (i) => {
console.log(j);
};
setTimeout(log, 100, j);
}
// example
console.log('one');
setTimeout(function () {
// this function will run in end after promise
console.log('two');
}, 0);
Promise.resolve().then(function () {
console.log('three');
});
console.log('four');
// one
// four
// three
// two
// example
var obj = {
id: 'awesome',
cool: function coolFn() {
console.log(this.id);
},
};
var id = 'not awesome';
obj.cool(); // awesome
// problem
setTimeout(obj.cool, 100); // undefined
// solution?
// const arr = [10, 12, 15, 21];
// for (var k = 0; k < arr.length; k++) {
// setTimeout(function() {
// console.log('Index: ' + k + ', element: ' + arr[k]);
// }, 1000);
// } // log 4 five times
// Closures can be used to prevent this problem by creating a unique scope for each iteration,
// storing each unique value of the variable within its scope, as follows:
// const arr = [10, 12, 15, 21];
// for (var l = 0; l < arr.length; l++) {
// // pass in the variable i so that each function
// // has access to the correct index
// setTimeout(function(i_local) {
// return function() {
// console.log('The index of this number is: ' + i_local);
// }
// }(l), 1000);
// } // log 0, 1, 2, 3
// Another way to solve the above issue is by using let keyword
const arr = [10, 12, 15, 21];
for (let a = 0; a < arr.length; a++) {
setTimeout(function () {
console.log('The index of this number is: ' + a);
}, 3000);
}
// Write a function that will loop through a list of integers and print the index of each element after a 3-second delay.
for (var b = 0; b < 3; b++) {
setTimeout(
(function (il) {
return function () {
console.log(il);
};
})(b),
1000 + b
);
}
for (var c = 0; c < 5; c++) {
setTimeout(function () {
console.log(c, 'c');
}, c * 1000);
}
for (var d = 0; d < 5; d++) {
(function (d) {
setTimeout(function () {
console.log(d, 'd');
}, d * 1000);
})(i);
}
for (var e = 0; e < 10; e++) {
setTimeout(console.log.bind(console, e, 'e'), 10);
}
for (var f = 0; f < 3; f++) {
setTimeout(function log() {
console.log(f, 'f');
}, 1000);
}
// example
function doAsyncTask(cb) {
setTimeout(
() => {
console.log('Async Task Calling Callback');
cb();
},
1000
);
}
doAsyncTask(() => console.log('Callback Called'));
// example
setTimeout(function cb1() {
console.log('cb1');
}, 5000);
let p1 = new Promise((resolve, reject) => {
resolve('foo');
});
let p2 = new Promise((resolve, reject) => {
reject('bar');
});
console.log('bip');
p1.then((val) => {
console.log(val);
return p2;
})
.then((val) => {
console.log('baz');
})
.catch((err) => {
console.log(err);
});
console.log('bop');
// bip
// bop
// foo
// bar
setTimeout(function () {
console.log('Welcome to system design after 2 seconds');
}, 2000);
var deliEmployeeName = 'Alex';
var customerName = 'Bailey';
setTimeout(function greetTheCustomer() {
console.log(
'Hello' +
customerName +
' my name is' +
deliEmployeeName +
'What can I get you today?'
);
}, 100);
var timesRun = 0;
var interval = setInterval(function () {
timesRun += 1;
if (timesRun === 4) {
clearInterval(interval);
}
console.log('timer');
}, 100);