-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeOutText.js
44 lines (34 loc) · 975 Bytes
/
TypeOutText.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
var text = "";
var typingFunction = null;
var typingCharacterId = 0;
var resultContainer = document.querySelector("#typingResults");
var typeButton = document.querySelector("#typeButton");
typeButton.addEventListener("click", typeButtonClicked);
function typeButtonClicked(){
typeOutText();
}
function typeOutText(){
window.clearInterval(typingFunction);
typingCharacterId = 0;
resultContainer.innerHTML = "";
text = getTypeText();
typingFunction = setInterval(printText, 100);
}
function getTypeText(){
var textInput = document.querySelector("#textInput");
return textInput.value;
}
function printText() {
var showText = "";
if(typingCharacterId <= text.length){
showText = text.charAt(typingCharacterId);
typingCharacterId++;
var textSpan = document.createElement('span');
textSpan.innerHTML = showText;
typingResults.appendChild(textSpan);
}
else{
window.clearInterval(typingFunction);
return;
}
}