I've made some changes in the previous code, running more tests, and I've run into the same problem again (Is it possible continue a stopped async function?). So this question is related.
Furthermore, the previous solution does not work.
var stop = false;
var pause = false;
var elParrafo = document.getElementById('miParrafo');
function sendPause() {
pause = true;
}
function sendContinue() {
pause = false;
}
function sendStop() {
stop = true;
}
function longWork(ms, char) {
return new Promise(function(resolve, reject) {
elParrafo.innerHTML += char;
setTimeout(resolve, ms);
});
}
async function doIt() {
for (var i = 0; i < 666; i++) {
if (!stop) {
if (!pause) {
await longWork(50," > ");
}
} else {
break;
}
}
}
doIt();
<form class="" action="index.html" method="post">
<input type="button" value="Pause" onclick="sendPause()">
<input type="button" value="Continue" onclick="sendContinue()">
<input type="button" value="Stop" onclick="sendStop()">
</form>
<p id="miParrafo"></p>