forked from Evgeny-Mufteev/Click
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
35 lines (25 loc) · 952 Bytes
/
main.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
let clicks = 1;
const TIMEOUT = 5000;
const display = document.querySelector('#display');
const button = document.querySelector('#button');
const counter = document.querySelector('#counter');
button.onclick = start;
function start() {
const startTime = Date.now();
display.textContent = formatTime(TIMEOUT);
button.onclick = () => counter.textContent = clicks++;
const interval = setInterval(() => {
const delta = Date.now() - startTime;
display.textContent = formatTime(TIMEOUT - delta);
}, 100);
const timeout = setTimeout(() => {
button.onclick = null;
display.textContent = 'Время вышло, игра окончена. Даже 33 не набрал?';
clearInterval(interval)
clearTimeout(timeout);
}, TIMEOUT);
};
// Уже лишнее, но решили усложнить
function formatTime(ms) {
return Number.parseFloat(ms / 1000).toFixed(2);
}