Sync timer update code

 <!DOCTYPE html>

<html>

<head>

    <title>Timer</title>

</head>

<body>

    <div id="timer"></div>


    <script>

        // Assuming 'totime' is retrieved from the database and is in milliseconds since epoch

        var totime = Date.now() + 30000; // Example: 30 seconds from now


        function updateTimer() {

            var now = Date.now();

            var diff = totime - now;


            if (diff <= 0) {

                document.getElementById('timer').innerHTML = '00:00';

                clearInterval(timerInterval); // Stop the timer when time is up

                return;

            }


            var minutes = Math.floor(diff / 60000);

            var seconds = Math.floor((diff % 60000) / 1000);


            document.getElementById('timer').innerHTML =

                (minutes < 10 ? '0' : '') + minutes + ':' + (seconds < 10 ? '0' : '') + seconds;

        }


        var timerInterval = setInterval(updateTimer, 1000); // Update every second

    </script>

</body>

</html>


Previous
Next Post »