Dear All,
I have managed to create a countdown timer that can be seen within the Wix Pages utilising JavaScript that runs from 1 minute to 0 seconds the code is below, I am needing to utilise this code to call a function that is further up within the page code on a regular basis (63000ms) could someone please help and advise of how to do this? I look forward to your responses. @yisrael-wix :
$w.onReady( function () {
$w(‘#update’).onClick(() => {
function countdown(timer, minutes, seconds) {
var time = minutes*60 + seconds;
var interval = setInterval( function () {
if (time <= -1) {
var text = “Updating”;
setTimeout( function () {
countdown(‘timer’, 0, 60);
}, 3000);
clearInterval(interval);
return $w(‘#timer’).text = text;
}
var minutes = Math.floor( time / 60 );
if (minutes < 10) minutes = “0” + minutes;
var seconds = time % 60;
if (seconds < 10) seconds = “0” + seconds;
time–;
return $w(‘#timer’).text = (minutes+‘:’+ seconds);
}, 1000);
}
countdown(‘timer’, 0, 60);
});
});
Hi Simon,
So, you want call a function when the countdown turns zero? You can just add an if function to verify if the counter is zeroed.
Something like this:
$w.onReady(function () {
$w('#update').onClick(() => {
function countdown(timer, minutes, seconds) {
var time = minutes * 60 + seconds;
var interval = setInterval(function () {
if (time <= -1) {
var text = "Updating";
setTimeout(function () {
countdown('timer', 0, 60);
}, 3000);
clearInterval(interval);
return $w('#timer').text = text;
}
var minutes = Math.floor(time / 60);
if (minutes < 10) minutes = "0" + minutes;
var seconds = time % 60;
if (seconds < 10) seconds = "0" + seconds;
time--;
// if the time runs out the function is performed
if (minutes == 0 && seconds == 0) {
console.log('call the function here')
}
return $w('#timer').text = (minutes + ':' + seconds);
}, 1000);
}
countdown('timer', 0, 10);
});
});
Hope it helps
Thanks for this @matheus I am trying to create a One Time Access Code (OTAC) similar to the TOTP available from google but independent and run within my wix website, I am now able to utilise the timer function to update 3 sets of random numbers and to save these within my data collection, what I would love to achieve is the ability to save the timer “time” within my collection? if possible and also so it can be recursive and run all the time and generating new random numbers every 60 seconds then my customers given the ability to call a new set of three codes using them as an access code to place into a remote login adding extra security for my customers.