91 lines
2.3 KiB
JavaScript
91 lines
2.3 KiB
JavaScript
|
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);
|
||
|
|
||
|
|
||
|
|
||
|
function Stopwatch(action) {
|
||
|
switch (action) {
|
||
|
case "start":
|
||
|
console.log("start function started");
|
||
|
if (previousAction == "start") { break; }
|
||
|
startTime = new Date();
|
||
|
curAction = "increase";
|
||
|
tick();
|
||
|
previousAction = "start";
|
||
|
break;
|
||
|
case "stop":
|
||
|
curAction = "stop";
|
||
|
tick();
|
||
|
previousAction = "stop";
|
||
|
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();
|
||
|
}
|