forked from kaleidawave/online-stopwatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
86 lines (78 loc) · 2.78 KB
/
script.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
var fullscreen = false;
var timer;
var running = true;
var days = 0;
var hours = 0;
var minutes = 0;
var seconds = 0;
//var diff = 100;
document.onkeydown = (e) => {
if (e.key === "s" || e.keyCode == 32) { ToggleTimer(); }
else if (e.key === "r") { ClearTimer(); }
else if (e.key === "f") { ToggleFullscreen(); }
}
function DrawTime() {
document.getElementById("day").innerText = `${days}d`;
document.getElementById("hour").innerText = `${hours}h`;
document.getElementById("minute").innerText = `${minutes}m`;
document.getElementById("second").innerText = `${seconds}s`;
// document.getElementById("second").innerText = `${seconds.toFixed(1)}s`;
// document.getElementById("debug").innerText = `${Math.random().toFixed(5)}, ${diff}, ${running.toString()}, ${seconds}`;
}
function ToggleFullscreen() {
var elem = document.documentElement;
if (!fullscreen) {
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.mozRequestFullScreen) { /* Firefox */
elem.mozRequestFullScreen();
} else if (elem.webkitRequestFullscreen) { /* Chrome, Safari and Opera */
elem.webkitRequestFullscreen();
} else if (elem.msRequestFullscreen) { /* IE/Edge */
elem.msRequestFullscreen();
}
document.getElementById("fullscreen-icon").innerText = "fullscreen_exit";
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) { /* Firefox */
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) { /* Chrome, Safari and Opera */
document.webkitExitFullscreen();
} else if (document.msExitFullscreen) { /* IE/Edge */
document.msExitFullscreen();
}
document.getElementById("fullscreen-icon").innerText = "fullscreen";
}
fullscreen = !fullscreen;
}
function ToggleTimer() {
if (!running) { StartTimer(); }
else { StopTimer(); }
running = !running;
}
async function StartTimer() {
//pre = Date.now() ;
//setInterval(() => {
//diff = Date.now() - pre ;
//}, 100);
//if (running == true) { seconds += (diff/1000) ; }
timer = setInterval(function () {
if (running == true) { seconds += 1 ; }
if (seconds >= 60) { seconds -= 60; minutes++; }
if (minutes >= 60) { minutes -= 60; hours++; }
if (hours >= 24) { hours -= 24; days++; }
DrawTime();
}, 1000);
document.getElementById("start-btn").innerText = "stop";
}
function StopTimer() {
clearInterval(timer);
running = false;
document.getElementById("start-btn").innerText = "start";
}
function ClearTimer() {
StopTimer();
days = 0, hours = 0, minutes = 0, seconds = 0;
DrawTime();
}