-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
64 lines (60 loc) · 2 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const testWrapper = document.querySelector('#test-area');
const testArea = document.querySelector('#test-area');
const originText = document.querySelector('#origin-text p').innerHTML;
const resetbtn = document.querySelector('#reset');
const theTimer = document.querySelector('.timer');
var timer = [0, 0, 0, 0];
var interval;
var timerRunning = false;
//leadingZero
function leadingZero(time){
if(time<=9)
{
time="0"+time;
}
return time;
}
//timer
function runTime() {
let currentTime = leadingZero(timer[0])+ ":" + leadingZero(timer[1]) + ":" + leadingZero(timer[2]);
theTimer.innerHTML = currentTime;
timer[3]++;
timer[0] = Math.floor((timer[3] / 100) / 60);
timer[1] = Math.floor((timer[3] / 100) - (timer[0] * 60));
timer[2] = Math.floor(timer[3] - (timer[1] * 100) - (timer[0] * 6000));
}
//Start function
function start() {
let textExtendLength = testArea.value.length;
if (textExtendLength === 1 && !timerRunning) {
timerRunning=true;
interval= setInterval(runTime, 10);
}
console.log(textExtendLength);
}
//spellCheck
function spellCheck() {
let textEntered = testArea.value;
let originTextMatch = originText.substring(0,textEntered.length);
if(textEntered == originText)
{
clearInterval(interval);
testWrapper.style.borderColor="#1C7C54";
}else if(textEntered==originTextMatch)
{
testWrapper.style.borderColor="#43f2Ff";
}else{
testWrapper.style.borderColor="#E84558";
}
}
function reset() {
clearInterval(interval);
interval=null;
timer=[0,0,0,0];
timerRunning=false;
testArea.value="";
theTimer.innerHTML="00:00:00";
testWrapper.style.borderColor=" #8D99AE";
}
//EventListners
testArea.addEventListener("keyup", start, false); testArea.addEventListener("keyup", spellCheck, false); resetbtn.addEventListener("click", reset, false);