count-up timer in wix
Stopwatch display
00:00:00:00
$w.onReady(function () {
$w("#startButton").onClick(function () {
start();
});
$w("#pauseButton").onClick(function () {
pauseTimer();
});
$w("#resetTimer").onClick(function () {
resetTimer();
});
$w("#startStop").onClick(function () {
startStop();
});
});
//▶TIMER
var x;
var startOrStop = 0;
//▶START AND STOP TIMER
function startStop() {
startOrStop = startOrStop + 1;
if (startOrStop === 1) {
start();
$w("#startStop").label = "Stop ⛔"
} else if (startOrStop === 2) {
startOrStop = 0;
pauseTimer();
$w("#startStop").label = "Start ▶"
}
}
//▶START TIMER
function start() {
x = setInterval(timer, 10);
}
//▶PAUSE TIMER
function pauseTimer() {
clearInterval(x);
}
var milisec = 0;
var sec = 0;
var min = 0;
var hr = 0;
var miliSeconds = 0;
var Seconds = 0;
var Minutes = 0;
var Hour = 0;
function timer() {
//▶MAIN TIMER
miliSeconds = checkTimerZeros(milisec);
Seconds = checkTimerZeros(sec);
Minutes = checkTimerZeros(min);
Hour = checkTimerZeros(hr);
milisec = ++milisec;
if (milisec === 100) {
milisec = 0;
sec = ++sec;
}
if (sec === 60) {
min = ++min;
sec = 0;
}
if (min === 60) {
min = 0;
hr = ++hr;
}
$w("#timer").text = `${Hour}:${Minutes}:${Seconds}:${miliSeconds}`;
}
function checkTimerZeros(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
//▶RESET TIMER
function resetTimer() {
milisec = 0;
sec = 0;
min = 0
hr = 0;
$w("#timer").text = `00:00:00:00`;
}
//END ▶