0

I have a digital clock made with jQuery, it displays current exact time from the client machine, it works perfectly, BUT I want to make it display the exact time from the server and not the client.

How it works it updates every 1000ms (1 second) and every time retrieves the current hour:minute:second

Now is there a way to make a HTTP request to a PHP script (/get-time.php) and display the exact server time? Of course it's not a good practice to make a HTTP request every second to get the new time for every visitor, so is there a way to make it even every 10 seconds to correct the time and keep the clock ticking every second like a real digital clock?

This is the HTML:

<div class="digital-clock float-right">00:00:00</div>

This is the jQuery code:

$(document).ready(function() {
    clockUpdate();
    setInterval(clockUpdate, 1000);
});

function clockUpdate() {
    var date = new Date();
    $('.digital-clock').css({'color': '#fff', 'text-shadow': '0 0 3px #ff0'});
    function addZero(x) {
        if (x < 10) {
            return x = '0' + x;
        } else {
            return x;
        }
    }

    function twelveHour(x) {
        if (x > 12) {
            return x = x - 12;
        } else if (x == 0) {
            return x = 12;
        } else {
            return x;
        }
    }

    var h = addZero(twelveHour(date.getHours()));
    var m = addZero(date.getMinutes());
    var s = addZero(date.getSeconds());

    $('.digital-clock').text(h + ':' + m + ':' + s)
}
medk
  • 9,233
  • 18
  • 57
  • 79
  • 1
    See if [this helps](https://stackoverflow.com/questions/6624239/clock-on-webpage-using-server-and-system-time). – El_Vanja May 26 '21 at 13:31
  • @El_Vanja thanks a lot this what I'm looking for, now I have to figure out how to make it work. – medk May 26 '21 at 13:43

0 Answers0