-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrtipt.js
48 lines (41 loc) · 1.03 KB
/
scrtipt.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
const textElement = document.getElementById('text');
const speedElement = document.getElementById('speed');
const texts = ['We are programmers.', 'We are the best.', 'We love programming.'];
let index = 1;
let wordIndex = 0;
let increase = true;
let speed = 300 / speedElement.value;
let step = speed;
speedElement.addEventListener('input', () => {
speed = 300 / speedElement.value;
});
writeText();
function writeText() {
textElement.innerText = texts[wordIndex].slice(0, index);
setTimeout(() => {
nextStep();
writeText();
}, step);
}
function nextStep() {
if (increase) {
index++;
// 50%-150% random step speed
step = Math.floor(speed * 0.5 + Math.random() * speed);
if (index >= texts[wordIndex].length) {
increase = false;
step = 1000;
}
} else {
index--;
step = speed / 5;
if (index <= 0) {
increase = true;
step = 1000;
wordIndex++;
if (wordIndex >= texts.length) {
wordIndex = 0;
}
}
}
}