2023-11-22 17:10:45 +03:00
|
|
|
var curAction = "wait";
|
|
|
|
var previousAction = "wait"
|
|
|
|
var startTime = new Date();
|
|
|
|
var curTime = new Date();
|
|
|
|
var timeMS = 0;
|
|
|
|
var FlagMilisecond = 1;
|
|
|
|
var timer = document.getElementById("stopwatch").getElementsByClassName("timer")[0];
|
|
|
|
|
|
|
|
tick();
|
|
|
|
|
|
|
|
// NOT OPTIMIZED, place in start switch
|
|
|
|
var t = setInterval(tick, 10);
|
|
|
|
|
2023-11-23 22:18:53 +03:00
|
|
|
const sleep = ms => new Promise(r => setTimeout(r, ms));
|
2023-11-22 17:10:45 +03:00
|
|
|
|
2023-11-23 22:18:53 +03:00
|
|
|
async function Stopwatch(action) {
|
2023-11-22 17:10:45 +03:00
|
|
|
switch (action) {
|
|
|
|
case "start":
|
|
|
|
console.log("start function started");
|
|
|
|
if (previousAction == "start") { break; }
|
|
|
|
startTime = new Date();
|
|
|
|
curAction = "increase";
|
|
|
|
tick();
|
|
|
|
previousAction = "start";
|
2023-11-23 22:18:53 +03:00
|
|
|
document.getElementById("start-cro").style.color = "greenyellow";
|
|
|
|
document.getElementById("start-cro").style.borderTopColor = "greenyellow";
|
|
|
|
document.getElementById("start-cro").style.borderLeftColor = "greenyellow";
|
2023-11-24 23:47:05 +03:00
|
|
|
await sleep(200);
|
2023-11-23 22:18:53 +03:00
|
|
|
document.getElementById("start-cro").style.color = "grey";
|
|
|
|
document.getElementById("start-cro").style.borderTopColor = "grey";
|
|
|
|
document.getElementById("start-cro").style.borderLeftColor = "grey";
|
2023-11-22 17:10:45 +03:00
|
|
|
break;
|
|
|
|
case "stop":
|
|
|
|
curAction = "stop";
|
|
|
|
tick();
|
|
|
|
previousAction = "stop";
|
2023-11-23 22:18:53 +03:00
|
|
|
document.getElementById("stop-cro").style.color = "red";
|
|
|
|
document.getElementById("stop-cro").style.borderTopColor = "red";
|
|
|
|
document.getElementById("stop-cro").style.borderLeftColor = "red";
|
2023-11-24 23:47:05 +03:00
|
|
|
await sleep(200);
|
2023-11-23 22:18:53 +03:00
|
|
|
document.getElementById("stop-cro").style.color = "white";
|
|
|
|
document.getElementById("stop-cro").style.borderTopColor = "white";
|
|
|
|
document.getElementById("stop-cro").style.borderLeftColor = "white";
|
|
|
|
document.getElementById("start-cro").style.color = "white";
|
|
|
|
document.getElementById("start-cro").style.borderTopColor = "white";
|
|
|
|
document.getElementById("start-cro").style.borderLeftColor = "white";
|
2023-11-22 17:10:45 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// console.error("no action (old script)");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateMsec() {
|
|
|
|
timer.getElementsByClassName("msec")[0].innerHTML = getDigits(Math.floor((timeMS / 10) % 100), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateSec() {
|
|
|
|
timer.getElementsByClassName("sec")[0].innerHTML = getDigits(Math.floor((timeMS / 1000) % 60), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateMin() {
|
|
|
|
timer.getElementsByClassName("min")[0].innerHTML = getDigits(Math.floor((timeMS / 1000 / 60) << 0), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getDigits(str, num) {
|
|
|
|
return ("0" + str).slice(-num);
|
|
|
|
}
|
|
|
|
|
|
|
|
function tick() {
|
|
|
|
switch (curAction) {
|
|
|
|
case "increase":
|
|
|
|
curTime = new Date();
|
|
|
|
timeMS = (curTime.getTime() - startTime.getTime());
|
|
|
|
if (FlagMilisecond) {
|
|
|
|
updateMsec();
|
|
|
|
}
|
|
|
|
updateSec();
|
|
|
|
updateMin();
|
|
|
|
break;
|
|
|
|
case "stop":
|
|
|
|
timer.getElementsByClassName("min")[0].innerHTML = "00";
|
|
|
|
timer.getElementsByClassName("sec")[0].innerHTML = "00";
|
|
|
|
timer.getElementsByClassName("msec")[0].innerHTML = "00";
|
|
|
|
curAction = "wait";
|
|
|
|
previousAction = "stop"
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// console.error("no action (old script)");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function toggleTimerMsec() {
|
|
|
|
let chx = document.getElementById("chx-timerMsec");
|
|
|
|
if (chx.checked) {
|
|
|
|
FlagMilisecond = 1;
|
|
|
|
timer.getElementsByClassName("msec")[0].style.display = "flex";
|
|
|
|
document.getElementById("stopwatch-msec-dot").style.display = "flex";
|
|
|
|
} else {
|
|
|
|
FlagMilisecond = 0;
|
|
|
|
timer.getElementsByClassName("msec")[0].style.display = "none";
|
|
|
|
document.getElementById("stopwatch-msec-dot").style.display = "none";
|
|
|
|
}
|
|
|
|
tick();
|
|
|
|
}
|