Hi, can annyone help me make a button in wix when user press it it starts displaying min and sec from 0:0 untill the button is pressed again and then it resets, hit the button again and it starts counting from 0:0 again ?
Thanks
Hi, can annyone help me make a button in wix when user press it it starts displaying min and sec from 0:0 untill the button is pressed again and then it resets, hit the button again and it starts counting from 0:0 again ?
Thanks
Add a button (“button1”) and a text element (“text1”).
And use something like:
let isRunning = false, counter = 0, runStopwatch;
$w.onReady(() => {
resetTexts();
$w("#button1").onClick(event => {
if (!isRunning) {
isRunning = true;
$w("#button1").label = "RESET";
runStopwatch = setInterval(count, 1000);
} else {
clearInterval(runStopwatch);
isRunning = false;
resetTexts();
counter = 0;
}
})
})
function resetTexts() {
$w("#text1").text = "00:00";
$w("#button1").label = "START";
}
function count() {
counter++;
let formattedTime = `${(Math.floor(counter/60).toString().padStart(2, '0'))}:${(counter % 60).toString().padStart(2, '0')}`;
$w("#text1").text = formattedTime;
}