JSFiddle - React, Tailwind, and code Playground
HTML
<input type="button" onclick="stop();" value="stop it!!!" />  
<input type="button" onclick="continueWithIt();" value="continue" />
<div id='foo'> </div>
JavaScript
var flag = true;
function foo(){
if (flag){
$('<span> working in the background </span> <br>').appendTo($('#foo'));
setTimeout(foo, 500);
}
}
function stop(){
flag = false;
}
function continueWithIt(){
flag = true;
foo();
}
foo();