I'm trying to learn about promises, and I wonder about to start and stop an async function, like my example show. It starts at the begining and stop when you click the button, but it seems can't continue.
var stop = 0;
var elParrafo = document.getElementById('miParrafo');
function sendStop() {
stop = 1;
}
function sendStart() {
stop = 0;
}
function esperar(ms) {
return new Promise(function(resolve, reject) {
setTimeout(resolve, ms)
});
}
async function trabajar() {
while (stop === 0) {
elParrafo.innerHTML += " > ";
await esperar(50);
}
}
trabajar();
<form class="" action="index.html" method="post">
<input type="button" value="Stop" onclick="sendStop()">
<input type="button" value="Start" onclick="sendStart()">
</form>
<p id="miParrafo"></p>