-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsimple.html
More file actions
122 lines (117 loc) · 3.27 KB
/
simple.html
File metadata and controls
122 lines (117 loc) · 3.27 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width">
<meta name="タイピングゲーム">
<title>タイピングゲーム</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<div class="container">
<div id="timer"></div>
</div>
<div class="typingContainer">
<p id="target" class="cursor"></p>
<p id="result"></p>
<p id="restart" class="cursor"></p>
</div>
<script type="module">
window.onload = async () => {
for (;;) {
target.textContent = "Click to Start!"
const t = new Timer(timer);
const words = ['red', 'blue', 'yellow'];
await waitClickOrEnter(target);
shuffle(words);
t.start();
for (let i = 0; i < words.length; i++) {
const word = words[i];
target.textContent = word;
for (let j = 0; j < word.length; j++) {
while (word[j] != await waitKeyDown());
target.textContent = '_'.repeat(j + 1) + word.substring(j + 1);
}
}
t.stop();
result.textContent = "Congratulations!!";
restart.textContent = "もう一度挑戦する";
await waitClickOrEnter(restart);
result.textContent = "";
restart.textContent = "";
}
};
// util
const waitClick = (comp) => {
return new Promise((resolve) => {
comp.classList.add("cursor");
comp.onclick = () => {
comp.onclick = null;
comp.classList.remove("cursor");
resolve();
}
})
};
const waitClickOrEnter = (comp) => {
return new Promise((resolve) => {
comp.classList.add("cursor");
document.onkeydown = (e) => {
document.onkeydown = null;
if (e.key == "Enter") {
resolve();
}
};
comp.onclick = () => {
comp.onclick = null;
comp.classList.remove("cursor");
resolve();
}
})
};
const waitKeyDown = () => {
return new Promise(resolve => {
document.onkeydown = (e) => {
document.onkeydown = null;
resolve(e.key);
}
});
};
const rnd = (n) => {
return Math.floor(Math.random() * n);
};
const shuffle = (array) => {
const len = array.length;
for (let i = 0; i < len; i++) {
const n = rnd(len);
const tmp = array[i];
array[i] = array[n];
array[n] = tmp;
}
};
class Timer {
constructor(div) {
this.div = div;
this.div.textContent = "00:00:000";
}
start() {
if (this.tid) {
this.stop();
}
this.tid = null;
this.startTime = new Date().getTime();
this.show();
this.tid = setInterval(() => {
this.show();
}, 10);
}
show() {
const d = new Date(Date.now() - this.startTime);
const m = d.getMinutes().toString().padStart(2, '0');
const s = d.getSeconds().toString().padStart(2, '0');
const ms = d.getMilliseconds().toString().padStart(3, '0');
this.div.textContent = `${m}:${s}:${ms}`;
}
stop() {
clearInterval(this.tid);
this.tid = null;
}
}
</script>
</body>
</html>