-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06response.html
More file actions
81 lines (77 loc) · 2.7 KB
/
06response.html
File metadata and controls
81 lines (77 loc) · 2.7 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>반응속도측정</title>
</head>
<style>
#screen {
width: 300px;
height: 200px;
text-align: center;
user-select: none;
}
#screen.waiting {
background-color: aqua;
}
#screen.ready {
background-color: red;
color: white;
}
#screen.now {
background-color: greenyellow;
}
</style>
<body>
<div id="screen" class="waiting">클릭해서 시작하세요</div>
<div id="result"></div>
<script>
const $screen = document.querySelector('#screen');
const $result = document.querySelector('#result');
let startTime; // let은 블록스코프, 변수는 함수 사용하고 날아간다.
let endTime;
const records = [];
let timeoutId;
$screen.addEventListener('click', (event) =>{
if (event.target.classList.contains('waiting')) { //파랑
$screen.classList.remove('waiting');
$screen.classList.add('ready');
$screen.textContent = '초록색이 되면 클릭하세요.'
timeoutId = setTimeout(function () {
startTime = new Date();
$screen.classList.remove('ready');
$screen.classList.add('now');
$screen.textContent = '클릭하세요!';
// 첫 시간 재기
}, Math.floor(Math.random() * 1000) + 2000); // 2000~3000 사이 수(2초~ 3초 사이)
} else if (event.target.classList.contains('ready')) { // 빨강
clearTimeout(timeoutId);
$screen.classList.remove('ready');
$screen.classList.add('waiting');
$screen.textContent = '너무 성급하시군요!';
} else if (event.target.classList.contains('now')) { // 초록
// 끝 시간 재기
// 시간 차이 저장하기
endTime = new Date();
const current = endTime - startTime;
records.push(current);
const average = records.reduce((a, c) => a + c) / records.length; // 평균 구하기 외워두면 좋음.
// reduce('축소하다') 개념 잘 알아두기(편리한 매서드).배열에서 사용하기 좋음.
// 초기값을 넣지 않을 때 첫자리가 초기값임.
$result.textContent = `현재 ${current}ms, 평균: ${average}ms`
const topFive = records.sort((p,c) => p-c).slice(0,5);
topFive.forEach((top, index) => {
$result.append(
document.createElement('br'),
`${index + 1}위: ${top}ms`,
);
});
startTime = null; // null 개념 확인하기.(초기화하는 용도)
endTime = null;
$screen.classList.remove('now');
$screen.classList.add('waiting');
$screen.textContent = '클릭해서 시작하세요';
}
});
</script>
</body>
</html>