I'm trying to set up counter logic for a game. The critical point here is that every user who enters this page sees the same second at that moment. I will have a counter that counts down from 25 and everyone will place their bets during this time. When the 25 seconds are over, a 10 second counter will start, during which 10 seconds the winning bet will be announced. How can I set up the database setup so that these counters are repeated continuously?
I am using the following code for javascript, but every time I enter the page it starts from 25
var interval = 25000;
var interval1 = 10000;
var endTime;
function millisToMinutesAndSeconds(millis) {
// Use floor instead of toFixed
var seconds = Math.floor((millis % 60000) / 1000);
return (seconds < 10 ? "0" : "") + seconds;
}
function reset() {
// Use Date.now() instead of +new Date()
// And create a cycle length that covers both intervals
endTime = Date.now() + interval + interval1;
}
reset();
setInterval(function () {
var remaining = endTime - Date.now();
if (remaining >= 0) {
// Adjust the time to display
// depending on where in the total interval we are:
if (remaining >= interval1) remaining -= interval1;
document.getElementById("timer").innerText =
millisToMinutesAndSeconds(remaining);
} else {
reset()
}
}, 100);