-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCTimer.js
More file actions
96 lines (81 loc) · 2.45 KB
/
CTimer.js
File metadata and controls
96 lines (81 loc) · 2.45 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
let startTime, endTime, running = false;
let timerInterval;
function generateScramble(){
const moves = ['R', 'L', 'U', 'D', 'F', 'B'];
const atributs = ["", "'", "2"];
let scramble = [];
let lastMove = '';
for (let i=0; i<20; i++){
let move;
do {
move = moves[Math.floor(Math.random()*moves.length)];
} while(move === lastMove);
lastMove = move;
let atribut = atributs[Math.floor(Math.random()*atributs.length)];
scramble.push(move+atribut)
};
return scramble.join(' ');
}
document.getElementById('scramble').innerText = '3x3 Scramble: \n' + generateScramble();
function startTimer(){
if(!running){
startTime = performance.now();
running = true;
timerInterval = requestAnimationFrame(updateTimer);
}
}
function stopTimer(){
if(running){
endTime = performance.now();
running = false;
let time = (endTime - startTime)/1000;
displayTime(time);
}
}
function updateTimer(){
if(running){
let currentTime = (performance.now() - startTime)/1000;
document.getElementById('timer').innerText = currentTime.toFixed(2)+'s';
timerInterval = requestAnimationFrame(updateTimer);
}
}
function displayTime(time){
document.getElementById('timer').innerText = time.toFixed(2)+'s';
}
document.getElementById('start').addEventListener('click', function(){
if(running){
stopTimer();
this.blur();
} else {
startTimer();
this.blur();
}
})
document.addEventListener('keydown', (event) =>{
const timer = document.getElementById('timer');
if(event.keyCode==32){
if(!running){
document.getElementById('timer').innerText = '0.00s';
timer.style.color = 'gold';
}
}
})
document.addEventListener('keyup', (event) =>{
if(event.keyCode==32){
if(running){
document.getElementById('scramble').innerText = '3x3 Scramble: \n' + generateScramble();
stopTimer();
} else{
document.getElementById('timer').innerText = '0.00s'
timer.style.color = '';
startTimer();
}
}
})
function newScramble(){
running = false;
cancelAnimationFrame(timerInterval);
document.getElementById('scramble').innerText = '3x3 Scramble: \n' + generateScramble();
this.blur();
}
document.getElementById('nextScramble').addEventListener('click', newScramble);