Skip to content

Commit 16f9e21

Browse files
committed
Merge pull request BonsaiDen#265 from SamMorrowDrums/master
Add an important example to Closures setTimeout
2 parents 30958da + 5f3bc27 commit 16f9e21

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

doc/en/function/closures.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ above.
9696
})(i), 1000)
9797
}
9898

99+
The other popular way to acheive this is to add an additional argument to
100+
the setTimeout function, which passes these arguments to the callback.
101+
102+
for(var i = 0; i < 10; i++) {
103+
setTimeout(function(e) {
104+
console.log(e);
105+
}, 1000, i);
106+
}
107+
108+
Some legacy JS environments (Internet Explorer 9 & below) do not support this.
109+
99110
There's yet another way to accomplish this by using `.bind`, which can bind
100111
a `this` context and arguments to function. It behaves identically to the code
101112
above

0 commit comments

Comments
 (0)