Clock shows server time, but on ajax with one request

How can I do it or maybe someone saw the source code of the finished solution.
The situation is as follows, the server time is displayed on the site
Moscow time:<?=date(“H”)?> : <?=date(“i”);? >

How can I do this in Ajax, as follows. The user visits the page, there is a request to show the time, and there is no more request. Does Javascript itself add seconds and change the time?

Answer 1, authority 100%

Let’s use the example @DemoS

The server must have a script to send server time:


  echo encode_json($servTime);

Next, use the functions @DemoSneeds to be redone so that they do not contact the server every second, but adding 1 to the seconds forms both minutes and hours ( here you will have to think it out yourself).

    var objTime;
   $.getJSON("time.php", function(data){

objTime – will be a “time” object. objTime.currentYear – will match the year, etc.

Next, we pass the objTime object to the clock() function;

Answer 2, authority 67%

Here is my implementation, only the time of the client goes right away, but I think it’s not a problem to undermine:
date and time (jQuery).

Answer 3, authority 33%

If absolute precision isn’t critical, you can do it a lot easier:

varserverDate; function timeStart() { timestamp = document.getElementById("moscowTime").innerHTML; serverDate = parseInt(timestamp) * 1000; setInterval("showTime()",1000); } function showTime() { serverDate = serverDate + 1000; date = new Date(serverDate); var time = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds(); var timeWrapper = document.getElementById("moscowTime"); timeWrapper.innerHTML = time; } onload = function() { timeStart(); }

<div id="moscowTime"><?=time();?></div>

PS: written without ajax because laid out a strict algorithm, of course, the accuracy will be higher if you take the timestamp with Ajax on onload.