setTimeout inside for loop with Interval 0 [duplicate] - javascript

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Is setTimeout with no delay the same as executing the function instantly?
(4 answers)
Closed 5 years ago.
when I print "i" inside setTimeout with time diff 0. why does it print "n" the same number always?
for(i = 0; i < 5; i++) {
setTimeout(function() {console.log(i)}, 0);
}
Output:
5
5
5
5
5

Related

How do I set a delay with setTimeout()? [duplicate]

This question already has answers here:
Why is the method executed immediately when I use setTimeout?
(8 answers)
What is the difference between a function call and function reference?
(6 answers)
Closed 9 months ago.
I have code that is supposed to make a one-second-delayed loop, but it instead executes the code in the loop constantly.
loop1()
function loop1() {
setTimeout(loop2(), 1000)
}
function loop2() {
console.log("hey")
setTimeout(myLoop(), 1000)
}

console.log() on setTimeout() JS [duplicate]

This question already has answers here:
setTimeout calls function immediately instead of after delay
(2 answers)
JavaScript setTimeout() won't wait to Execute? [duplicate]
(3 answers)
setTimeout in for-loop does not print consecutive values [duplicate]
(10 answers)
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 4 years ago.
can someone help me understanding why this code:
for (var i =0; i < 2; i++) {
setTimeout(console.log(i), 0);
}
console.log("aaa");
Will write:
0
1
aaa
But that code:
for (var i =0; i < 2; i++) {
setTimeout(function(){console.log(i)}, 0);
}
console.log("aaa");
Will wirte that:
aaa
2
2
Note that I understand how the second vers. work, I don't get why the first one make it differnt.
Thanks!

Asynchronous scope [duplicate]

This question already has answers here:
Explanation of `let` and block scoping with for loops
(5 answers)
Difference between let and var inside setTimeout? [duplicate]
(3 answers)
Why let and var bindings behave differently using setTimeout function? [duplicate]
(2 answers)
Closed 5 years ago.
There is something I didn't really understand clearly, If somebody could help me:
let arr = [1,2,3,4,5,6,7];
let b = 0
for (let a of arr) {
setTimeout(() => {
console.log(b, a)
}, 2000)
b = b + 1;
}
/* Output
7 1
7 2
7 3
7 4
7 5
7 6
7 7
*/
Let's say b is equal to 7 because 2 seconds later, the variable b is equal to 7, then why a has a different behaviour than b ?
The difference is how scoped variables work with the let keyword. In this case, you are defining a new a for each iteration of the loop. But it is the same b variable for every iteration.
The timeouts don't actually start firing until after the loop ends. By this time, each timeout function scope was given a different a, but are grabbing the same final value of b.

Why does the first loop result to 0 instead of 1? [duplicate]

This question already has answers here:
trivial for-loop needs an explanation
(4 answers)
Closed 5 years ago.
I don't understand why the first loop prints out 0 instead of 1. Doesn't the i++ apply to the first loop?
for (i = 0; i < 3; i++) {
console.log('i');
}
No i++ increments only after executing logic in the loop

How to bind your variables to setTimeout in javascript? [duplicate]

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 5 years ago.
for(i = 0; i < 10; i++){
setTimeOut(function(){
console.log(i);
},2000);
}
When I execute this, its printing 10, 10 times instead of 1,2,3....10. How do I fix this
You need to modify your code like this
function print(i){
console.log(i);
}
for(i=0;i<10;i++){
setTimeout(print.bind(null,i),2000);
}

Categories