JSFiddle - React, Tailwind, and code Playground
JavaScript
//log the numbers 1-10 in the console with one second intervals using a loop
for (var i = 0; i <= 10; i++) {
(function(index) {
setTimeout(function(){
console.log(index);
}, 1000 * index);
})(i);
}
//the above uses an IIFE, an immediately invoked function expression
//the IIFE takes in its own private copy of i
//it console logs the index at 1000ms, 2000ms, 3000ms, etc
//IIFE is the quickest way of creating a closure