JSFiddle - React, Tailwind, and code Playground
HTML
<input type="button" id="button" value="Click">
<div id="land">
</div>
JavaScript
i = 0; // global i
var make_new = function(data){
var $div = $('<div>', {'class': 'element', 'id':'element_'+data});
var func = function(){
// closure has formed around data, $div, and func
// so references to them here reference the local versions
// (the versions outside this immediate function block. i.e,
// inside this call of make_new)
data++;
$div.text($div.attr('id') + ":" + data);
setTimeout(func, 1000);
};
func(); //start process rolling
return $div;
};
$(document).ready(function(){
$("#button").on("click", function(){
i++;
var _i = i; //make local i
$("#land").append(make_new(_i));
});
});