Skip to content

Instantly share code, notes, and snippets.

@karenpeng
Last active October 6, 2015 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karenpeng/f87448d9db6e1b4b459c to your computer and use it in GitHub Desktop.
Save karenpeng/f87448d9db6e1b4b459c to your computer and use it in GitHub Desktop.
for(var i = 0; i < 5; i++){
setTimeout(function(){
//console.log(i)
}, i * 1000)
}
for(var i = 0; i < 5; i++){
(function(i){
setTimeout(function(){
console.log(i)
}, i * 1000)}(i))
}
for(var i = 0; i < 5; i++){
(function(i){
setTimeout(function(){
console.log(i)
}, i * 1000)
}.bind(this, i))()
}
for(var i = 0; i < 5; i++){
setTimeout((function(i, j){
return function(){
console.log(i)
}
})(i, 0), i * 1000)
}
for (let i = 0; i < arr.length; i++) {
delayLog(i)
}
function delayLog(i) {
setTimeout(function () {
console.log(i)
}, i * 1000)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment