JSFiddle - React, Tailwind, and code Playground
HTML
<div id="results"></div>
JavaScript
(function(){
var results = false;
var callbackList = [];
// just a simple method to print the given value
function printIt(it){
if(!results) results = document.getElementById('results');
results.appendChild(document.createTextNode(','+it));
}
// store up some callbacks to be executed
for(var i=0; i < 10; i++){
callbackList.push(
// use an IIFE to maintain the current value of the variable
(function(i){
return function(){
// print the current value of the i variable
printIt(i);
};
})(i)
);
}
// loop through and execute callbacks
for(var callback in callbackList){
if(!callbackList.hasOwnProperty(callback)) continue;
callbackList[callback].call(this);
}
})();