function SetText(gg = `textttttt `, cmd = `sudo --info`) {
window.scrollTo({ top: 0, behavior: 'smooth' });
if (document.getElementsByClassName('demo').length > 0) {
var i = 0;
var speed = 60;
document.getElementsByClassName('demo')[0].innerHTML = `<code class="shell-session demo hljs nginx"><span class="hljs-attribute">Website</span> <span class="hljs-regexp">~ $</span> ${cmd}`;
function typeItOut() {
if (i < gg.length) {
document.getElementsByClassName('demo')[0].innerHTML += gg.charAt(i);
i++;
setTimeout(typeItOut, speed);
}
}
setTimeout(typeItOut, 1800);
}
}
so that's the code, I want every time I click a button on my website it waits until the recursive finish then starts another one...
Can you use
async/await?If you can, this will make it much easier to in effect "pause" each iteration through your string by a given timeout duration (see the
handleIterateStringclass function below).This async
handleIterateStringfunction will "pause" at eachawaitkeyword, and wait until the promise returned by theawaitexpression has been resolved. Only then will it continue executing theasyncfunction.Also, you can "pause" the execution of the
asyncfunction where you initiate a new complete iteration through your string (see theawait demo.handleIterateStringcall inside theasync function SetTextbelow.In this way, you can wait for the entire iteration (i.e. typing behaviour) to finish before decrementing your click queue count.
If you have click events left in your queue, you can at that point call
SetTextrecursively.Simply put: using
async/awaitmakes it much easier to both control the speed of the typing behaviour, and wait for your typing behaviour to complete before doing anything else.Try running the code snippet below.