Unexpected behavior: Javascript, setTimeout(), and IIFE
Javascript, Event loop, setTimeout, IIFE, closure
Based on references below, my understanding of the following code is:
setTimeout() is non-blocking and handled by the Browser Web APIs, which put the callbacks on the callback queue when the timer is done. Then the event loop waits for the call stack to be free to run each callback in turn. setTimeout closure closes over the anonymous IIFE and has the correct value of index for each iteration.
for(var i = 0; i < 3; i++){
(function(index){
setTimeout(function(){
console.log(index);
}, 5000);
})(i);
console.log("loop="+i);
}
/*Output in console is
loop=0
loop=1
loop=2
//after 5 seconds
0
1
2
*/
I'm looking for an explanation of what's happening with the following code in Chrome.
for (var i = 0; i < 3; i++) {
setTimeout(
function(index) {
console.log(index);
}(i), 5000
);
console.log("loop="+i);
}
/* Output in console without any delay is:
0
loop=0
1
loop=1
2
loop=2
*/
Why is 'console.log(index)' executed immediately, without a 5 second delay?
How does the web API execute setTimeout() with a callback as an IIFE?
Are any callbacks put in the callback queue?
Does the event loop move any callbacks to the call stack?
Or is setTimeout() being ignored and its callback being executed immediately on the call stack?
References I've consulted:
Philip Roberts: What the heck is the event loop anyway? | JSConf EU 2014
https://www.youtube.com/watch?v=8aGhZQkoFbQ
Philip Roberts Help I'm stuck in an event loop 2016
https://www.youtube.com/watch?v=6MXRNXXgP_0
Call Stack & Event Loop
https://www.youtube.com/watch?v=mk0lu9MKBto
JavaScript closure inside loops – simple practical example
Use IIFE in setTimeout in a loop, but why?
javascript closures settimeout iife
add a comment |
Javascript, Event loop, setTimeout, IIFE, closure
Based on references below, my understanding of the following code is:
setTimeout() is non-blocking and handled by the Browser Web APIs, which put the callbacks on the callback queue when the timer is done. Then the event loop waits for the call stack to be free to run each callback in turn. setTimeout closure closes over the anonymous IIFE and has the correct value of index for each iteration.
for(var i = 0; i < 3; i++){
(function(index){
setTimeout(function(){
console.log(index);
}, 5000);
})(i);
console.log("loop="+i);
}
/*Output in console is
loop=0
loop=1
loop=2
//after 5 seconds
0
1
2
*/
I'm looking for an explanation of what's happening with the following code in Chrome.
for (var i = 0; i < 3; i++) {
setTimeout(
function(index) {
console.log(index);
}(i), 5000
);
console.log("loop="+i);
}
/* Output in console without any delay is:
0
loop=0
1
loop=1
2
loop=2
*/
Why is 'console.log(index)' executed immediately, without a 5 second delay?
How does the web API execute setTimeout() with a callback as an IIFE?
Are any callbacks put in the callback queue?
Does the event loop move any callbacks to the call stack?
Or is setTimeout() being ignored and its callback being executed immediately on the call stack?
References I've consulted:
Philip Roberts: What the heck is the event loop anyway? | JSConf EU 2014
https://www.youtube.com/watch?v=8aGhZQkoFbQ
Philip Roberts Help I'm stuck in an event loop 2016
https://www.youtube.com/watch?v=6MXRNXXgP_0
Call Stack & Event Loop
https://www.youtube.com/watch?v=mk0lu9MKBto
JavaScript closure inside loops – simple practical example
Use IIFE in setTimeout in a loop, but why?
javascript closures settimeout iife
As a side note, if you want to pass arguments to setTimeout, you need to put them after the delay argument, e.g. setTimeout(function (index) { /* do stuff */ }, 5000, i);
– Ray Chan
Jan 2 at 4:36
add a comment |
Javascript, Event loop, setTimeout, IIFE, closure
Based on references below, my understanding of the following code is:
setTimeout() is non-blocking and handled by the Browser Web APIs, which put the callbacks on the callback queue when the timer is done. Then the event loop waits for the call stack to be free to run each callback in turn. setTimeout closure closes over the anonymous IIFE and has the correct value of index for each iteration.
for(var i = 0; i < 3; i++){
(function(index){
setTimeout(function(){
console.log(index);
}, 5000);
})(i);
console.log("loop="+i);
}
/*Output in console is
loop=0
loop=1
loop=2
//after 5 seconds
0
1
2
*/
I'm looking for an explanation of what's happening with the following code in Chrome.
for (var i = 0; i < 3; i++) {
setTimeout(
function(index) {
console.log(index);
}(i), 5000
);
console.log("loop="+i);
}
/* Output in console without any delay is:
0
loop=0
1
loop=1
2
loop=2
*/
Why is 'console.log(index)' executed immediately, without a 5 second delay?
How does the web API execute setTimeout() with a callback as an IIFE?
Are any callbacks put in the callback queue?
Does the event loop move any callbacks to the call stack?
Or is setTimeout() being ignored and its callback being executed immediately on the call stack?
References I've consulted:
Philip Roberts: What the heck is the event loop anyway? | JSConf EU 2014
https://www.youtube.com/watch?v=8aGhZQkoFbQ
Philip Roberts Help I'm stuck in an event loop 2016
https://www.youtube.com/watch?v=6MXRNXXgP_0
Call Stack & Event Loop
https://www.youtube.com/watch?v=mk0lu9MKBto
JavaScript closure inside loops – simple practical example
Use IIFE in setTimeout in a loop, but why?
javascript closures settimeout iife
Javascript, Event loop, setTimeout, IIFE, closure
Based on references below, my understanding of the following code is:
setTimeout() is non-blocking and handled by the Browser Web APIs, which put the callbacks on the callback queue when the timer is done. Then the event loop waits for the call stack to be free to run each callback in turn. setTimeout closure closes over the anonymous IIFE and has the correct value of index for each iteration.
for(var i = 0; i < 3; i++){
(function(index){
setTimeout(function(){
console.log(index);
}, 5000);
})(i);
console.log("loop="+i);
}
/*Output in console is
loop=0
loop=1
loop=2
//after 5 seconds
0
1
2
*/
I'm looking for an explanation of what's happening with the following code in Chrome.
for (var i = 0; i < 3; i++) {
setTimeout(
function(index) {
console.log(index);
}(i), 5000
);
console.log("loop="+i);
}
/* Output in console without any delay is:
0
loop=0
1
loop=1
2
loop=2
*/
Why is 'console.log(index)' executed immediately, without a 5 second delay?
How does the web API execute setTimeout() with a callback as an IIFE?
Are any callbacks put in the callback queue?
Does the event loop move any callbacks to the call stack?
Or is setTimeout() being ignored and its callback being executed immediately on the call stack?
References I've consulted:
Philip Roberts: What the heck is the event loop anyway? | JSConf EU 2014
https://www.youtube.com/watch?v=8aGhZQkoFbQ
Philip Roberts Help I'm stuck in an event loop 2016
https://www.youtube.com/watch?v=6MXRNXXgP_0
Call Stack & Event Loop
https://www.youtube.com/watch?v=mk0lu9MKBto
JavaScript closure inside loops – simple practical example
Use IIFE in setTimeout in a loop, but why?
javascript closures settimeout iife
javascript closures settimeout iife
asked Jan 2 at 2:30
Alex_BAlex_B
96841222
96841222
As a side note, if you want to pass arguments to setTimeout, you need to put them after the delay argument, e.g. setTimeout(function (index) { /* do stuff */ }, 5000, i);
– Ray Chan
Jan 2 at 4:36
add a comment |
As a side note, if you want to pass arguments to setTimeout, you need to put them after the delay argument, e.g. setTimeout(function (index) { /* do stuff */ }, 5000, i);
– Ray Chan
Jan 2 at 4:36
As a side note, if you want to pass arguments to setTimeout, you need to put them after the delay argument, e.g. setTimeout(function (index) { /* do stuff */ }, 5000, i);
– Ray Chan
Jan 2 at 4:36
As a side note, if you want to pass arguments to setTimeout, you need to put them after the delay argument, e.g. setTimeout(function (index) { /* do stuff */ }, 5000, i);
– Ray Chan
Jan 2 at 4:36
add a comment |
3 Answers
3
active
oldest
votes
In
setTimeout(
function(index) {
console.log(index);
}(i), 5000
);
You're invoking the first argument passed to setTimeout
immediately. When the interpreter comes across the setTimeout
line, it first tries to resolve all of its arguments to values. The first argument is a function invocation, so it invokes that function in the expectation that it will resolve to another function - just like how one could do
setTimeout(makeFn('foo'), 5000);
where makeFn
returns a function.
So, in your code, the
function(index) {
console.log(index);
}(i)
runs immediately, but it doesn't return anything - the interpreter resolves the setTimeout
line to
setTimeout(undefined, 5000);
but undefined
isn't a function, so nothing asynchronous gets queued up.
You don't have any IIFEs here - put the whole setTimeout
line in an IIFE instead:
for (var i = 0; i < 3; i++) {
((i) => {
setTimeout(
function() {
console.log(i);
}, 500
);
console.log("loop=" + i);
})(i);
}
(or, of course, use const
or let
instead of var
- best to avoid var
, its hoisting and function scope is very unintuitive and requires verbose workarounds like these in for
loops)
for (let i = 0; i < 3; i++) {
setTimeout(
function() {
console.log(i);
}, 500
);
console.log("loop=" + i);
}
add a comment |
In second example you are not passing the function to the setTimeout
but you rather passing it's result of the function call (in this case it's void).
function(index) {
console.log(index);
}(i)
you see, in this example your function invokes immediately thus, there's nothing to call later, and console logs in order.
add a comment |
I'm looking for an explanation of what's happening with the following
code in Chrome.
for (var i = 0; i < 3; i++) {
setTimeout(
function(index) {
console.log(index);
}(i), 5000
);
console.log("loop="+i);
}
/* Output in console without any delay is:
0
loop=0
1
loop=1
2
loop=2
*/
Consider the following statement:
function(index) {
console.log(index);
}(i)
This is an anonymous function and executed immediately (the parentheses '()' at the end executes the function): see the syntax function(param) {...}()
. So the effect is that for each iteration the above code is executed immediately.
The result is (as you see it):
0
1
2
The setTimeout at MDN. method expects a function (to be executed after the timer expires) or code as its first parameter. In this case you have code (not a function) that executes immediately. So, you see the result immediately.
The delay of 5 seconds has no effect, its never used. There is nothing to execute after the delay.
The effect is same in Firefox too.
You can try the code without the parentheses at the end of the anonymous function and see what happens:
function(index) {
console.log(index);
}
The function will execute after the delay of five seconds, in this case!
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000574%2funexpected-behavior-javascript-settimeout-and-iife%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
In
setTimeout(
function(index) {
console.log(index);
}(i), 5000
);
You're invoking the first argument passed to setTimeout
immediately. When the interpreter comes across the setTimeout
line, it first tries to resolve all of its arguments to values. The first argument is a function invocation, so it invokes that function in the expectation that it will resolve to another function - just like how one could do
setTimeout(makeFn('foo'), 5000);
where makeFn
returns a function.
So, in your code, the
function(index) {
console.log(index);
}(i)
runs immediately, but it doesn't return anything - the interpreter resolves the setTimeout
line to
setTimeout(undefined, 5000);
but undefined
isn't a function, so nothing asynchronous gets queued up.
You don't have any IIFEs here - put the whole setTimeout
line in an IIFE instead:
for (var i = 0; i < 3; i++) {
((i) => {
setTimeout(
function() {
console.log(i);
}, 500
);
console.log("loop=" + i);
})(i);
}
(or, of course, use const
or let
instead of var
- best to avoid var
, its hoisting and function scope is very unintuitive and requires verbose workarounds like these in for
loops)
for (let i = 0; i < 3; i++) {
setTimeout(
function() {
console.log(i);
}, 500
);
console.log("loop=" + i);
}
add a comment |
In
setTimeout(
function(index) {
console.log(index);
}(i), 5000
);
You're invoking the first argument passed to setTimeout
immediately. When the interpreter comes across the setTimeout
line, it first tries to resolve all of its arguments to values. The first argument is a function invocation, so it invokes that function in the expectation that it will resolve to another function - just like how one could do
setTimeout(makeFn('foo'), 5000);
where makeFn
returns a function.
So, in your code, the
function(index) {
console.log(index);
}(i)
runs immediately, but it doesn't return anything - the interpreter resolves the setTimeout
line to
setTimeout(undefined, 5000);
but undefined
isn't a function, so nothing asynchronous gets queued up.
You don't have any IIFEs here - put the whole setTimeout
line in an IIFE instead:
for (var i = 0; i < 3; i++) {
((i) => {
setTimeout(
function() {
console.log(i);
}, 500
);
console.log("loop=" + i);
})(i);
}
(or, of course, use const
or let
instead of var
- best to avoid var
, its hoisting and function scope is very unintuitive and requires verbose workarounds like these in for
loops)
for (let i = 0; i < 3; i++) {
setTimeout(
function() {
console.log(i);
}, 500
);
console.log("loop=" + i);
}
add a comment |
In
setTimeout(
function(index) {
console.log(index);
}(i), 5000
);
You're invoking the first argument passed to setTimeout
immediately. When the interpreter comes across the setTimeout
line, it first tries to resolve all of its arguments to values. The first argument is a function invocation, so it invokes that function in the expectation that it will resolve to another function - just like how one could do
setTimeout(makeFn('foo'), 5000);
where makeFn
returns a function.
So, in your code, the
function(index) {
console.log(index);
}(i)
runs immediately, but it doesn't return anything - the interpreter resolves the setTimeout
line to
setTimeout(undefined, 5000);
but undefined
isn't a function, so nothing asynchronous gets queued up.
You don't have any IIFEs here - put the whole setTimeout
line in an IIFE instead:
for (var i = 0; i < 3; i++) {
((i) => {
setTimeout(
function() {
console.log(i);
}, 500
);
console.log("loop=" + i);
})(i);
}
(or, of course, use const
or let
instead of var
- best to avoid var
, its hoisting and function scope is very unintuitive and requires verbose workarounds like these in for
loops)
for (let i = 0; i < 3; i++) {
setTimeout(
function() {
console.log(i);
}, 500
);
console.log("loop=" + i);
}
In
setTimeout(
function(index) {
console.log(index);
}(i), 5000
);
You're invoking the first argument passed to setTimeout
immediately. When the interpreter comes across the setTimeout
line, it first tries to resolve all of its arguments to values. The first argument is a function invocation, so it invokes that function in the expectation that it will resolve to another function - just like how one could do
setTimeout(makeFn('foo'), 5000);
where makeFn
returns a function.
So, in your code, the
function(index) {
console.log(index);
}(i)
runs immediately, but it doesn't return anything - the interpreter resolves the setTimeout
line to
setTimeout(undefined, 5000);
but undefined
isn't a function, so nothing asynchronous gets queued up.
You don't have any IIFEs here - put the whole setTimeout
line in an IIFE instead:
for (var i = 0; i < 3; i++) {
((i) => {
setTimeout(
function() {
console.log(i);
}, 500
);
console.log("loop=" + i);
})(i);
}
(or, of course, use const
or let
instead of var
- best to avoid var
, its hoisting and function scope is very unintuitive and requires verbose workarounds like these in for
loops)
for (let i = 0; i < 3; i++) {
setTimeout(
function() {
console.log(i);
}, 500
);
console.log("loop=" + i);
}
for (var i = 0; i < 3; i++) {
((i) => {
setTimeout(
function() {
console.log(i);
}, 500
);
console.log("loop=" + i);
})(i);
}
for (var i = 0; i < 3; i++) {
((i) => {
setTimeout(
function() {
console.log(i);
}, 500
);
console.log("loop=" + i);
})(i);
}
for (let i = 0; i < 3; i++) {
setTimeout(
function() {
console.log(i);
}, 500
);
console.log("loop=" + i);
}
for (let i = 0; i < 3; i++) {
setTimeout(
function() {
console.log(i);
}, 500
);
console.log("loop=" + i);
}
answered Jan 2 at 2:36
CertainPerformanceCertainPerformance
93.7k165484
93.7k165484
add a comment |
add a comment |
In second example you are not passing the function to the setTimeout
but you rather passing it's result of the function call (in this case it's void).
function(index) {
console.log(index);
}(i)
you see, in this example your function invokes immediately thus, there's nothing to call later, and console logs in order.
add a comment |
In second example you are not passing the function to the setTimeout
but you rather passing it's result of the function call (in this case it's void).
function(index) {
console.log(index);
}(i)
you see, in this example your function invokes immediately thus, there's nothing to call later, and console logs in order.
add a comment |
In second example you are not passing the function to the setTimeout
but you rather passing it's result of the function call (in this case it's void).
function(index) {
console.log(index);
}(i)
you see, in this example your function invokes immediately thus, there's nothing to call later, and console logs in order.
In second example you are not passing the function to the setTimeout
but you rather passing it's result of the function call (in this case it's void).
function(index) {
console.log(index);
}(i)
you see, in this example your function invokes immediately thus, there's nothing to call later, and console logs in order.
answered Jan 2 at 2:41


UmaUma
37615
37615
add a comment |
add a comment |
I'm looking for an explanation of what's happening with the following
code in Chrome.
for (var i = 0; i < 3; i++) {
setTimeout(
function(index) {
console.log(index);
}(i), 5000
);
console.log("loop="+i);
}
/* Output in console without any delay is:
0
loop=0
1
loop=1
2
loop=2
*/
Consider the following statement:
function(index) {
console.log(index);
}(i)
This is an anonymous function and executed immediately (the parentheses '()' at the end executes the function): see the syntax function(param) {...}()
. So the effect is that for each iteration the above code is executed immediately.
The result is (as you see it):
0
1
2
The setTimeout at MDN. method expects a function (to be executed after the timer expires) or code as its first parameter. In this case you have code (not a function) that executes immediately. So, you see the result immediately.
The delay of 5 seconds has no effect, its never used. There is nothing to execute after the delay.
The effect is same in Firefox too.
You can try the code without the parentheses at the end of the anonymous function and see what happens:
function(index) {
console.log(index);
}
The function will execute after the delay of five seconds, in this case!
add a comment |
I'm looking for an explanation of what's happening with the following
code in Chrome.
for (var i = 0; i < 3; i++) {
setTimeout(
function(index) {
console.log(index);
}(i), 5000
);
console.log("loop="+i);
}
/* Output in console without any delay is:
0
loop=0
1
loop=1
2
loop=2
*/
Consider the following statement:
function(index) {
console.log(index);
}(i)
This is an anonymous function and executed immediately (the parentheses '()' at the end executes the function): see the syntax function(param) {...}()
. So the effect is that for each iteration the above code is executed immediately.
The result is (as you see it):
0
1
2
The setTimeout at MDN. method expects a function (to be executed after the timer expires) or code as its first parameter. In this case you have code (not a function) that executes immediately. So, you see the result immediately.
The delay of 5 seconds has no effect, its never used. There is nothing to execute after the delay.
The effect is same in Firefox too.
You can try the code without the parentheses at the end of the anonymous function and see what happens:
function(index) {
console.log(index);
}
The function will execute after the delay of five seconds, in this case!
add a comment |
I'm looking for an explanation of what's happening with the following
code in Chrome.
for (var i = 0; i < 3; i++) {
setTimeout(
function(index) {
console.log(index);
}(i), 5000
);
console.log("loop="+i);
}
/* Output in console without any delay is:
0
loop=0
1
loop=1
2
loop=2
*/
Consider the following statement:
function(index) {
console.log(index);
}(i)
This is an anonymous function and executed immediately (the parentheses '()' at the end executes the function): see the syntax function(param) {...}()
. So the effect is that for each iteration the above code is executed immediately.
The result is (as you see it):
0
1
2
The setTimeout at MDN. method expects a function (to be executed after the timer expires) or code as its first parameter. In this case you have code (not a function) that executes immediately. So, you see the result immediately.
The delay of 5 seconds has no effect, its never used. There is nothing to execute after the delay.
The effect is same in Firefox too.
You can try the code without the parentheses at the end of the anonymous function and see what happens:
function(index) {
console.log(index);
}
The function will execute after the delay of five seconds, in this case!
I'm looking for an explanation of what's happening with the following
code in Chrome.
for (var i = 0; i < 3; i++) {
setTimeout(
function(index) {
console.log(index);
}(i), 5000
);
console.log("loop="+i);
}
/* Output in console without any delay is:
0
loop=0
1
loop=1
2
loop=2
*/
Consider the following statement:
function(index) {
console.log(index);
}(i)
This is an anonymous function and executed immediately (the parentheses '()' at the end executes the function): see the syntax function(param) {...}()
. So the effect is that for each iteration the above code is executed immediately.
The result is (as you see it):
0
1
2
The setTimeout at MDN. method expects a function (to be executed after the timer expires) or code as its first parameter. In this case you have code (not a function) that executes immediately. So, you see the result immediately.
The delay of 5 seconds has no effect, its never used. There is nothing to execute after the delay.
The effect is same in Firefox too.
You can try the code without the parentheses at the end of the anonymous function and see what happens:
function(index) {
console.log(index);
}
The function will execute after the delay of five seconds, in this case!
edited Jan 2 at 7:27
answered Jan 2 at 7:13


prasad_prasad_
1,5681718
1,5681718
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000574%2funexpected-behavior-javascript-settimeout-and-iife%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
As a side note, if you want to pass arguments to setTimeout, you need to put them after the delay argument, e.g. setTimeout(function (index) { /* do stuff */ }, 5000, i);
– Ray Chan
Jan 2 at 4:36