Skip to content

Commit 1ae2fb7

Browse files
closure practice
1 parent 9125dc1 commit 1ae2fb7

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

Closures/setTimeOutPractice.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
function x() {
3+
let a = 10;
4+
let b = 20;
5+
setTimeout(function() {
6+
console.log(a + b); // 30
7+
}, 3000);
8+
console.log('This will run first'); // This will run first
9+
// setTimeout is asynchronous, so it will not block the execution of the next line
10+
}
11+
//x();
12+
13+
// setTimeout is a function that takes a callback and a delay in milliseconds
14+
// The callback will be executed after the specified delay
15+
// In this case, the callback is an anonymous function that logs the sum of a and b
16+
// The delay is 3000 milliseconds (3 seconds)
17+
18+
19+
function y(){
20+
for(var i = 1; i <= 5; i++){
21+
setTimeout(() => {
22+
console.log(i);
23+
}, timeout = i * 1000);
24+
}
25+
}
26+
//y();
27+
28+
// This will log 5 five times because the loop completes before the timeouts execute
29+
// The value of i is 6 when the timeouts execute, so it logs 6
30+
// why?
31+
// The reason is that var is function-scoped, not block-scoped
32+
// When the loop finishes, the value of i is 6, and all the timeouts will log 6
33+
// To fix this, we can use let instead of var, which is block-scoped
34+
// or we can use an IIFE (Immediately Invoked Function Expression) to capture the value of i at each iteration
35+
// Example using IIFE
36+
function z() {
37+
for (var i = 1; i <= 5; i++) {
38+
function close(i){
39+
setTimeout(() =>{
40+
console.log(i);
41+
}, i * 1000);
42+
}
43+
close(i); // Pass i to the IIFE
44+
// This will create a new scope for each iteration of the loop
45+
// and capture the current value of i
46+
// The IIFE will be executed immediately, and the value of i will be passed to it
47+
// The setTimeout function will then use this captured value of i
48+
// This way, each timeout has its own copy of i, and they log the expected values
49+
// this will log 1, 2, 3, 4, 5 as expected
50+
}
51+
}
52+
z();
53+
// The IIFE captures the current value of i at each iteration and passes it to the set
54+
// timeout function as iCopy
55+
// This way, each timeout has its own copy of i, and they log the expected values
56+
// Note: Using let instead of var would also fix the issue without needing an IIFE
57+
// Example using let
58+
function a() {
59+
for (let i = 0; i < 5; i++) {
60+
setTimeout(() => {
61+
console.log(i);
62+
}, i * 1000);
63+
}
64+
}
65+
//a();
66+
// This will also log 0, 1, 2, 3, 4 as expected
67+
// The let keyword creates a block scope, so each iteration of the loop has its own i variable
68+
// This is a common issue when using var in loops with asynchronous functions
69+
// The problem with the original code is that the variable i is declared with var, which is function-scoped
70+
// and not block-scoped. This means that by the time the setTimeout callback executes, the loop has already completed,
71+
// and the value of i is 5 for all callbacks.
72+
// This is a common pitfall in JavaScript when using var in loops with asynchronous functions.
73+
// The original code logs 5 five times because the loop completes before the timeouts execute.
74+
// The value of i is 5 when the timeouts execute, so it logs 5.
75+
// This is a common pitfall in JavaScript when using var in loops with asynchronous functions.
76+
// The original code logs 5 five times because the loop completes before the timeouts execute.
77+
// The value of i is 5 when the timeouts execute, so it logs 5.
78+
// This is a common pitfall in JavaScript when using var in loops with asynchronous functions.
79+
// The original code logs 5 five times because the loop completes before the timeouts execute.
80+
// The value of i is 5 when the timeouts execute, so it logs 5
81+
// This is a common pitfall in JavaScript when using var in loops with asynchronous functions.
82+
83+

0 commit comments

Comments
 (0)