-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
41 lines (34 loc) · 1.04 KB
/
script.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
class WriteMachine {
constructor(phrasesArr, htmlElement, speed) {
this.phrases = phrasesArr;
this.text = htmlElement;
this.speed = speed;
this.i = 0;
this.j = 0;
this.isDeleting = false;
this.actualPhrase = [];
}
alternatePhrases() {
if (this.i < this.phrases.length) {
if(this.j <= this.phrases[this.i].length && !this.isDeleting) {
this.actualPhrase.push(this.phrases[this.i][this.j])
this.text.innerHTML = this.actualPhrase.join('')
this.j++
}
if (this.isDeleting) {
this.speed = 50
if (this.j === 0) {
this.isDeleting = false
this.i = (this.i + 1) === this.phrases.length ? 0 : (this.i + 1)
} else {
this.actualPhrase.pop()
this.j--
this.text.innerHTML = this.actualPhrase.join('')
}
}
if (this.j === this.phrases[this.i].length) this.isDeleting = true
}
setTimeout(this.alternatePhrases.bind(this), this.speed)
}
}
export default WriteMachine