-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypewritter.js
More file actions
59 lines (50 loc) · 1.44 KB
/
Typewritter.js
File metadata and controls
59 lines (50 loc) · 1.44 KB
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
51
52
53
54
55
56
57
58
59
customElements.define(
"type-writter",
class extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
const typewritter = document.createElement("span");
typewritter.classList.add("typewritter");
this.appendChild(typewritter);
const link = document.createElement("link");
link.rel = "stylesheet";
link.href = "Typewritter.css";
this.appendChild(link);
const texts = [
"Software Engineer",
"Lifelong Learner",
"Linux Enthusiast",
"Musician",
"Leader and Team Player",
"Home Cook",
];
let textIndex = 0;
let charIndex = 0;
const typeSpeed = 80;
const eraseSpeed = 50;
const delayBetweenTexts = 1000;
const writeText = () => {
if (charIndex < texts[textIndex].length) {
typewritter.innerHTML += texts[textIndex].charAt(charIndex);
charIndex++;
setTimeout(writeText, typeSpeed);
} else {
setTimeout(deleteText, delayBetweenTexts);
}
};
const deleteText = () => {
if (charIndex > 0) {
typewritter.innerHTML = texts[textIndex].substring(0, charIndex - 1);
charIndex--;
setTimeout(deleteText, eraseSpeed);
} else {
textIndex = (textIndex + 1) % texts.length;
setTimeout(writeText, delayBetweenTexts);
}
};
writeText();
}
}
);