Skip to content

Commit e57bd1f

Browse files
author
juana pu
authored
Merge branch 'master' into master
2 parents 985da57 + 9b974a1 commit e57bd1f

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

js/setTimeout-inside-loop.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,22 @@ for(var i=0;i<5;i++){
8181
}
8282
//use call/bind method(2)
8383
for(var i=0;i<5;i++){
84+
//use bind/call to achieve this function, method(1)
85+
for(var i=0;i<10;i++){
86+
setTimeout(console.log.bind(null,i),1000);
87+
}
88+
//use bind/call to achieve this function, method(2)
89+
for(var i=0;i<10;i++){
8490
setTimeout(function(index){
8591
console.log(index);
8692
}.bind(null,i),1000);
8793
}
8894
//use call/bind method(3)
95+
//use bind/call to achieve this function, method(3)
8996
for(var i=0;i<10;i++){
9097
setTimeout(function(index){
9198
return function(){
9299
console.log(index);
93100
}
94101
}.call(null,i),1000);
95102
}
96-
97-

0 commit comments

Comments
 (0)