-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (131 loc) · 4.1 KB
/
index.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
let held = 0;
let threshold = 300; //default threshold is 300
let timer_on = false;
let time = 0; //all times in ms
let times = [];
let key_is_down = false;
let col_changed = false;
let change_text = true;
let lightmode = false;
let start_time = Date.now();
let cur_interval;
let timer = document.getElementById("timer");
let list = document.getElementById("listwrap");
let list_size = 0;
document.addEventListener("keydown",handle_down);
document.addEventListener("keyup",handle_up);
// setInterval(() => {console.log("Held ",held," ",start_time)},1000);
//handles keydown event
function handle_down(event){
if(key_is_down) return;
key_is_down = true;
display_time();
if(event.key==" " && event.target==document.body){
event.preventDefault();
}
if(timer_on){
clearInterval(cur_interval);
timer_on = false;
held = 0;
console.log("stopped!");
times.push(time);
let new_time = document.createElement("div");
new_time.classList.add("time_rec");
new_time.style.flex = 1;
new_time.style.marginLeft = 10;
if(list_size==0) new_time.id = "first";
else if(list_size>=3) {
new_time.id = "last";
list.style.boxShadow = "inset 0px 0px 5px black";
}
++list_size;
new_time.innerHTML = ""+list_size+":";
new_time.appendChild(document.createElement("br"));
new_time.innerHTML+=display_time();
list.insertBefore(new_time,list.firstChild);
}else if(event.key==" "){
held = 0;
start_time = Date.now();
cur_interval = setInterval(upd,10);
}
}
//handles keyup event
function handle_up(event){
key_is_down = false;
display_time();
clearInterval(cur_interval);
if(event.key==" " && !timer_on && held>=threshold){
timer_on = true;
start_time = Date.now();
cur_interval = setInterval(upd,10);
console.log("started!");
}
held = 0;
if(lightmode) {
if(change_text){
timer.style.color = "black";
timer.style.textShadow = "none";
timer.style.boxShadow = "none";
}else {
document.body.style.backgroundColor = "white";
}
}else {
if(change_text){
timer.style.color = "white";
timer.style.textShadow = "none";
timer.style.boxShadow = "none";
}else {
document.body.style.backgroundColor = "black";
}
}
col_changed = false;
}
//records time or time key held down
function upd(){
// console.log(held,timer_on);
if(!timer_on && key_is_down){
// console.log(held,col_changed);
if(held>=threshold && !col_changed) {
if(change_text){
timer.style.color = "green";
timer.style.textShadow = "0px 0px 40px green";
timer.style.boxShadow = "0px 0px 80px green, 0px 0px 40px green";
//timer.style.boxShadow = "0px 0px 40px green";
}else {
document.body.style.backgroundColor = "green";
}
col_changed = true;
}
held = Date.now()-start_time;
}else if(timer_on && !key_is_down){
time = Date.now()-start_time;
display_time();
}
}
function display_time(){
let rounded = Math.floor(time/10);
let display = ""+(rounded/100);
if(rounded%100==0) display+=".00";
else if(rounded%10==0) display+="0";
timer.innerHTML = display+" s";
return display+"s";
}
//add toggle dark mode
let button = document.getElementById("btn");
button.addEventListener("click",toggle);
let title = document.getElementById("tle");
toggle();
function toggle(){
console.log("toggle is called!");
if(lightmode){
document.body.style.backgroundColor = "black";
let arr = document.querySelectorAll("body *");
arr.forEach(x => {x.style.color = "white"});
}else{
document.body.style.backgroundColor = "white";
let arr = document.querySelectorAll("body *");
arr.forEach(x => {x.style.color = "black"});
}
button.style.color = "black";
lightmode = !lightmode;
}