for (let i = 0; i < 10; i++) {
// debugger
var log = function () {
// debugger
console.log(i)
};
var logWrappper = function () {
console.log(i);
log();
}
// logWrappper();
setTimeout(logWrappper, 10)
}
I have code like above, if I invoke logWrapper
directly,the console.log(i)
outputs the same result(0-9) both in log
and logWrapper
, however if I pass it to a setTimeout and invoke it later, console.log(i)
in logWrapper remains the same(0-9) while it outputs 10 times 9 in log
, can anybody explain it? thank you very much.