MaketTahta/assets/js/timers.js

82 lines
1.9 KiB
JavaScript
Raw Normal View History

2023-10-26 01:26:55 +03:00
// OLD TIMER SCRIPT !!!
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.warn("Stopwatch() is working, possible unoptimized, WIP!");
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);
}
// no need, already in utils.js
// function getDigits(str, num) {
// return ("0" + str).slice(-num);
// }
function tick() {
switch (curAction) {
case "increase":
curTime = new Date();
timeMS = (curTime.getTime() - startTime.getTime());
if (confs.get('timerMsec') == "1") {
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.warn("tick() is working, possible unoptimized, WIP!");
break;
}
}