JSFiddle - React, Tailwind, and code Playground
JavaScript
(function() {
var i, j;
function callbackFactory(j) {
//the `j` inside this execution context enters it as a formal parameter,
//shadowing the outer `j`. That is, it is independent from the outer `j`.
//You could name the parameter as "k" and use "k" when logging, for example.
return function() {
//scope chain will sick the closest `j` in above scopes, that being
//the one from the callbackFactory's scope in which this returned
//function has been initialized.
//It will also seek up the "closest" `i`,
//which is scoped inside the outer wrapper IIFE.
console.log("in timeout i is: " + i + " j is: " + j);
};
}
for(i = 0; i < 5; i++) {
j = i + 10;
console.log("i is: " + i + " j is: " + j);
setTimeout( callbackFactory(j), i * 1000);
}
}());