xxxxxxxxxx
// This solution is extracted from: https://www.geeksforgeeks.org/how-to-pause-and-play-a-loop-in-javascript-using-event-listeners/
// The below function can pause the flow of execution of javascript program
// Our approach in this program for pausing and playing a loop is same as delaying a loop using Promise, but instead of resolving the Promise after some specific duration, we will resolve Promise by event listeners.
const playBtn = document.getElementById("play");// if user clicks on this button then the execution on loop will be resumed
const pauser = () => {
return new Promise((resolve, reject) => {
playBtn.addEventListener("click", () => {
resolve(true);
});
});
};
// How to call pauser function?
// You will need to using async/await syntax to pause the flow of program using pauser
const myLoop = async () => {
for (let i = 0; i < 5; i++) {
console.log(i);
await pauser();
}
};