Skip to content

Instantly share code, notes, and snippets.

@whatrocks
Created July 15, 2020 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whatrocks/a64067225f4f5e9c5b4eeff616c88a95 to your computer and use it in GitHub Desktop.
Save whatrocks/a64067225f4f5e9c5b4eeff616c88a95 to your computer and use it in GitHub Desktop.
Print string array one char at a time
<html>
<body>
<div id="lines"></div>
<script type="text/javascript" src="./slow.js"></script>
</body>
</html>
const lines = ["I am the first line", "I am second line!"];
async function printLines() {
let el = document.getElementById("lines");
for (let line of lines) {
let p = document.createElement("p");
el.appendChild(p);
s = "";
for (let char of line) {
await delay(100);
s += char;
p.innerText = s;
}
}
}
async function delay(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
printLines();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment