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
handleIterateString
class function below).This async
handleIterateString
function will "pause" at eachawait
keyword, and wait until the promise returned by theawait
expression has been resolved. Only then will it continue executing theasync
function.Also, you can "pause" the execution of the
async
function where you initiate a new complete iteration through your string (see theawait demo.handleIterateString
call inside theasync function SetText
below.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
SetText
recursively.Simply put: using
async/await
makes 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.