Skip to content

Instantly share code, notes, and snippets.

@DeMoorJasper
Last active January 23, 2018 15:39
Show Gist options
  • Save DeMoorJasper/305ec3f7a6ffcfd82e691ce49de1a935 to your computer and use it in GitHub Desktop.
Save DeMoorJasper/305ec3f7a6ffcfd82e691ce49de1a935 to your computer and use it in GitHub Desktop.
TerminalTyper
const BLOCK_CHAR = '█';
let blockChar = `${BLOCK_CHAR}`;
function updateTerminal(content) {
document.getElementById('terminal').innerHTML = content;
}
function flickerBlock() {
if (blockChar.length > 0) {
blockChar = "";
} else {
blockChar = `${BLOCK_CHAR}`;
}
}
function writeText(text, content, count) {
if (content.lastIndexOf(BLOCK_CHAR) > 0) {
content = content.substring(0, content.lastIndexOf(BLOCK_CHAR));
}
content += text[count] + blockChar;
updateTerminal(content);
window.setTimeout(() => {
flickerBlock();
writeText(text, content, count + 1);
}, 50);
}
document.addEventListener("DOMContentLoaded", function(event) {
const text = document.getElementById('someText').value;
writeText(text, "", 0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment