Currently the JavaScript runs perfectly fine on one click. However, when I click the "Test Test" button while the function is already running, it will seemingly interrupt itself. I want the second click to queue itself up and wait until the "Thank you!" goes away. After the "Thank you!" from the first click goes away, then, and only then, I want the second click to run the function again.
This should be for all subsequent clicks. If I click the button 5 times in a row very quickly, the full function should play through 5 times, in order, as opposed to overlapping itself and only typing one "Thank you".
Here is the JS in demo form: https://jsfiddle.net/p562thwe/
let egg = document.querySelector("#egg");
let petEffects = document.querySelector("#streamPet");
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
document.querySelector("#button").onclick = async function demo() {
egg.classList.add("bounce");
await sleep(3000);
egg.classList.remove("bounce");
petEffects.innerText = `Wowee!`;
petEffects.classList.add("typing");
await sleep(2000);
petEffects.innerText = ``;
petEffects.classList.remove("typing");
await sleep(100);
petEffects.innerText = `Thank you!`;
petEffects.classList.add("typing");
await sleep(3000);
petEffects.innerText = ``;
petEffects.classList.remove("typing");
}
demo();
I have tried adding a boolean, but I couldn't get it working, nor do I think it would 'queue' up the next instances.