JSFiddle - React, Tailwind, and code Playground
HTML
<button id="a">Stop a</button>
<button id="b">Stop b</button>
JavaScript
var closure = function(id) {
var n = 0;
var go = true;
$('#' + id).one('click', function(event) {
go = false;
});
var foo = function() {
if(go) {
console.log(id, n++);
setTimeout(foo, 1000);
}
};
foo();
};
closure('a');
closure('b');