Skip to content

Instantly share code, notes, and snippets.

@somandubey
Created February 5, 2017 14:51
Show Gist options
  • Save somandubey/6c612f41417815422a244f63db557d2c to your computer and use it in GitHub Desktop.
Save somandubey/6c612f41417815422a244f63db557d2c to your computer and use it in GitHub Desktop.
function foo(){
for (var i=1;i<6;i++) {
var f = function(i) {
var k = function(){console.log(i)};
setTimeout(k,1000*i);
}
f(i);
}
}
foo();
@somandubey
Copy link
Author

somandubey commented Feb 5, 2017

original problem -

function foo() { for (var i=1;i<6;i++) { setTimeout(function() { console.log(i) }, 1000*i); } } foo();

Need to print 1 to 5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment