spatest/js/stopwatch.js

48 lines
1.1 KiB
JavaScript

var isStarted=false;
var number=0;
var noLaps=0;
const counter=document.getElementById('counter');
const startStop2 = document.getElementById('startStopBtn');
const laps = document.getElementById('laps');
function startStop()
{
isStarted = !isStarted;
startStop2.innerText = isStarted ? "Stop" : "Start";
}
function number2ts(number)
{
var ms = number % 1000;
var totalsec = (number - ms) / 1000;
var sec = totalsec % 60;
var totalMin = (totalsec - sec) / 60;
var min = totalMin % 60;
var totalHour = (totalMin - min) / 60;
return `${totalHour}:${min.toString().padStart(2,'0')}:${sec.toString().padStart(2,'0')}.${ms.toString().padStart(3,'0')}`;
}
function lap()
{
noLaps++;
laps.innerText += `${noLaps}: ${number2ts(number)}\n`;
}
function reset()
{
counter.innerText=`0:00:00.000`;
noLaps = 0;
number = 0;
laps.innerText="";
}
function tick()
{
if(isStarted)
{
number+=25;
counter.innerText = number2ts(number);
}
setTimeout(tick,25);
}
setTimeout(tick,25);