-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (40 loc) · 1.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function getConsoleContent() {
return console.innerHTML;
}
function appendToConsoleContent(str) {
console.innerHTML += str;
}
function setConsoleContent(str) {
console.innerHTML = str;
}
function lastCharacterIsCursor(consoleContent) {
return consoleContent.slice(-1) === '|';
}
function removeLastCharacter(consoleContent) {
setConsoleContent(consoleContent.slice(0, -1));
}
function addNextCharacter() {
index += SPEED;
setConsoleContent(text.slice(0, index));
window.scrollBy(0, 50);
}
function toggleCursor() {
const consoleContent = getConsoleContent();
if (lastCharacterIsCursor(consoleContent)) {
removeLastCharacter(consoleContent);
} else {
appendToConsoleContent(CURSOR);
}
}
const CURSOR = '|';
const SPEED = 3;
const console = document.getElementById('dynamic-console');
const text = document.getElementById('console-content').innerHTML.trim();
let index = 0;
const typer = setInterval(() => {
addNextCharacter();
if (index > text.length) {
clearInterval(typer);
}
}, 30);
setInterval(() => toggleCursor(), 500);