Repository navigation
Expand file tree
/
Copy pathhtmlinsertion.js
More file actions
493 lines (356 loc) · 9.92 KB
/
Copy pathhtmlinsertion.js
File metadata and controls
493 lines (356 loc) · 9.92 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// first.insertAdjacentHTML('beforeend', '<div class = "test">beforeend</div>');
// first.insertAdjacentHTML('afterend', '<div class = "test">afterend</div>');
// NODE REMOVAL
// first.remove()
// first.className = "text-black red"
// // REMOVING A CLASS
// first.classList.remove("red")
// console.log(first.classList)
// // ADDING A CLASS
// first.classList.add("red")
// console.log(first.classList)
// // ADD OR REMOVE
// first.classList.toggle("red")
// // TO CHECK FOR THE GIVEN CLASS
// console.log(first.classList.contains("red"))
// console.log(first.classList.contains("text-black"))
// // SETTIMEOUT AND SETINTERVAL
// // alert("hello")
// let a = setTimeout(function() {
// alert("I am inside of setTimeout")
// }, 2000)
// // console.log(a)
// let b = prompt("do you want to run the setTimeout")
// if ("no" == b) {
// clearTimeout(a)
// }
// clearTimeout(a)
// const sum = (a, b) => {
// console.log("yes I am rnning" + (a + b))
// a + b
// }
// setTimeout(sum, 4000, 1, 2)
// console.log(sum)
// let k = setInterval(function() {
// alert("I am inside of setInterval")
// }, 2000)
// clearInterval(k)
// INTRODUCTION TO BROWSER EVENTS
// let a = document.getElementsByClassName("container")[0]
// a.onclick= ()=>{
// let b = document.getElementsByClassName("container")[0]
// b.innerHTML = "Hello World"
// }
// EVENT LISTENER
// let x = function(e) {
// alert("Hello World1")
// }
// let y = function(e) {
// alert("Hello World2")
// }
// btn.addEventListener('click', x)
// btn.addEventListener('click', y)
// let a = prompt("What is your favourite number?")
// if (a == "2") {
// btn.removeEventListener('click', y)
// }
// let x = function (e) {
// // alert("Hello World1")
// console.log(e.clientX, e.clientY, e.clientZ)
// }
// let y = function (e) {
// // alert("Hello World2")
// console.log(e)
// }
// btn.addEventListener('click', x)
// btn.addEventListener('click', y)
// // let a = prompt("What is your favourite number?")
// // if (a == "2") {
// // btn.removeEventListener('click', x)
// // }
// // console.log("Start")
// // setTimeout(function() {
// // console.log("Hey I am good");
// // }, 3000)
// // console.log("End")
// // CALLBACK FUNCTIONS
// function loadScript(src, callback) {
// var script = document.createElement('script');
// script.src = src;
// script.onload = function () {
// console.log("I am appended successfully")
// callback();
// }
// script.onerror = function () {
// alert("Sorry your script can not be loaded")
// }
// document.body.appendChild(script);
// }
// function hello() {
// alert("Hello World")
// }
// setTimeout(function (lakka) {
// alert("Mai Nameer lakka hu")
// }, 8000)
// function greeting() {
// alert("Good Morning")
// }
// loadScript("https://cdn.jsdelivr.net/npm/bootstrap@5.3.5/dist/js/bootstrap.bundle.min.js", hello(), lakka())
// setTimeout(function () {
// alert("I am inside of setTimeout")
// }, 2000)
// let p = new Promise((resolve, reject) => {
// console.log("promise is pending")
// setTimeout(() => {
// console.log("I am promise and I am rejected")
// // resolve(true)
// reject(new Error("I am an error"))
// }, 5000)
// })
// console.log(p)
// let p = new Promise((resolve, reject) => {
// console.log("promise is pending")
// setTimeout(() => {
// console.log("I am promise and I am fulfilled")
// resolve(true)
// // reject(new Error("I am an error"))
// }, 5000)
// })
// console.log(p)
// p.then((value)=>{
// console.log(value)
// })
// let p1 = new Promise((resolve, reject)=>{
// setTimeout(()=>{
// console.log("Resolved after 2 sconds")
// // resolve(56)
// reject(new Error("It's an error"))
// }, 2000)
// })
// console.log(p1)
// p1.then((value)=>{
// console.log(value)
// })
// p1.catch((error)=>{
// console.log(error)
// })
// PROMISE CHAINING
// let p1 = new Promise((rseolve, reject)=>{
// setTimeout(()=>{
// console.log("Resolved after 2 seconds")
// rseolve(56)
// }, 2000)
// })
// p1.then((value)=>{
// console.log(value)
// let p2 = new Promise((rseolve, reject)=>{
// rseolve("Promise 2")
// })
// return p2
// })
// .then((value)=>{
// console.log("We are done")
// console.log(value)
// let p3 = new Promise((resolve, reject)=>{
// resolve("Now we are pakka done")
// })
// return 2
// return p3
// }).then((value)=>{
// console.log(value)
// })
// }).then((value)=>{
// console.log(value)
// })
// ATTACHING MULTIPLE HANDLERS TO A PROMISE
// let p1 = new Promise((resolve, reject)=>{
// setTimeout(()=>{
// console.log("Hey I am resolved")
// resolve(1)
// }, 2000)
// })
// p1.then((value)=>{
// console.log(value)
// return new Promise((resolve, reject)=>{
// setTimeout(()=>{
// resolve(4)
// }, 3000)
// }).then((value)=>{
// console.log("this is resolved")
// console.log(value)
// console.log("congratulations this promise is now resolved")
// })
// })
// p1.then(()=>{
// console.log("Hurray")
// })
// PROMISE API
// p1 = new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve(786)
// }, 2000)
// })
// p2 = new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve(7)
// }, 3000)
// })
// p3 = new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve(3)
// }, 4000)
// })
// p1.then((value)=>{
// console.log(value)
// })
// p2.then((value)=>{
// console.log(value)
// })
// p3.then((value)=>{
// console.log(value)
// })
// let promise_all = Promise.all([p1, p2, p3])
// promise_all.then((value) => {
// console.log(value)
// })
// ASSYNC/AWAIT
// async function lakka() {
// let delhiweather = new Promise((resolve, reject)=>{
// setTimeout(()=>{
// resolve("27 degrees")
// }, 3000)
// })
// let bangloreweather = new Promise((resolve, reject)=>{
// setTimeout(()=>{
// resolve("21 degrees")
// }, 6000)
// })
// delhiweather.then((value)=>{
// alert("weather of delhi is" + value)
// })
// bangloreweather.then((value)=>{
// alert("weather of banglore is" + value)
// })
// OR
// delhiweather.then(alert)
// bangloreweather.then(alert)
// console.log("Fetching Delhi weather please wait....")
// let delhiw = await delhiweather
// console.log("Delhi's weather has been fetched successfully" + delhiw)
// console.log("Fetching Banglore weather please wait....")
// let banglorew = await bangloreweather
// console.log("Banglore's weather has been fetched successfully" + banglorew)
// return [delhiw, banglorew]
// let delhiw = await delhiweather
// let banglorew = await bangloreweather
// return [delhiw, banglorew]
// console.log(delhiw)
// console.log(banglorew)
// }
// console.log(lakka())
// const staralien = () => {
// console.log("Hey I am staralien nd I am not waiting")
// }
// let a = lakka()
// let b = staralien()
// a.then((value)=>{
// console.log(value)
// })
// FINALLY CLAUSE
// try{
// let a =0
// console.log(p)
// console.log("program ran succesfully")
// }
// catch(error) {
// console.log(o)
// console.log("this is an error")
// }
// finally {
// console.log("I am a good boy yay!")
// }
// async function lakka() {
// let p1 = new Promise((resolve, reject) => {
// // console.log("this is your javascript file")
// setTimeout(() => {
// resolve("hello")
// }, 3000)
// })
// p1.then((value) => {
// console.log(value)
// })
// let p1a = await p1
// console.log(p1a)
// }
// try {
// console.log("No Error")
// console.log(error)
// }
// catch (error){
// console.log(kfewofjsfjeo)
// console.log("abey chutiye error aa gya")
// }
// finally {
// console.log("Isko try kar chutiye")
// }
// async function fetchingData () {
// try{
// let lmao = await fetch('https://jsonplaceholder.typicode.com/posts')
// let data = lmao.json
// console.log(lmao)
// console.log("after fetching")
// } catch (error) {
// console.error("Error fetching Data....")
// }}
// fetchingData()
// async function annu() {
// let a = new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve(456)
// }, 5000)
// })
// a.then((value) => {
// console.log(value)
// })
// const b = console.log("Hey I am under async function")
// let b1a = await b
// console.log(b1a)
// }
// annu()
// a.then((value)=>{
// console.log(value)
// })
async function demo() {
console.log("Demo has been started")
let p = await new Promise((resolve, reject) => {
setTimeout(() => {
resolve(786)
console.log("Timeout has finished")
}, 3000)
})
// let c = await p
// console.log(c)
console.log("The demo has been finished")
}
demo()
// SOLUTION FROM CHAT GPT
// let p = new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve(786)
// console.log("Timeout has finished")
// }, 4000)
// })
// p.then((value) =>{
// console.log(value)
// })
// async function example() {
// console.log("start")
// await new Promise (resolve => {
// setTimeout(()=>{
// console.log("Timeout finished")
// resolve()
// }, 2000)
// })
// console.log("end")
// }
// example()