Closure counter inside setInterval
问题 I have a function: setInterval(function () { var counter = 0; (function() { counter = counter + 1; console.log(counter); })(counter) }, 1000) Why does not it increment the counter? (instead, it logs 1's). How to make it log ascending numbers? (1, 2, 3, ....) 回答1: You are passing an argument to your anonymous function, but you aren't assigning that argument to a variable. You forgot to put the arguments in the function definition. You are creating new variables with every iteration instead of