-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
39 lines (31 loc) · 991 Bytes
/
Copy pathscript.js
File metadata and controls
39 lines (31 loc) · 991 Bytes
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
const textEl = document.querySelector('.text')
const speeedEl = document.querySelector('.speed')
const changeText = document.querySelector('.changeText')
let text = "This sentence is magically typing itself !"
let idx = 1
let speed = 300 / speeedEl.value
writeText()
function writeText(){
textEl.innerText = text.slice( 0, idx )
idx++
if(idx > text.length){
idx = 1
}
setTimeout(() => writeText(), speed);
}
speeedEl.addEventListener('input', (e)=>{
if(e.target.value>5){
e.target.value = 5
}
speed = 300/ e.target.value
//When user is trying to input speed value and the input remains empty the speed value is set to one which resets the time to default of 300ms and prevents the speed value to become Infinity
if(speed==Infinity){
speed = 300
}
})
changeText.addEventListener('input', (e)=> {
text = e.target.value
if(text.length === 0){
text = "Type something in there my dude"
}
})