When the time must come from the server rather than the browser, request a small endpoint at a controlled interval. Clear the timer when the component is no longer needed.
async function updateServerClock() {
const response = await fetch('ajax-server-clock-demock.php', {cache: 'no-store'});
if (!response.ok) return;
document.getElementById('msg').textContent = await response.text();
}
updateServerClock();
const clockTimer = setInterval(updateServerClock, 1000);The examples below show the same server-clock request with XMLHttpRequest so you can compare both AJAX approaches.
To display changing clock showing server time is possible by using Ajax where we can display a clock by sending request to server and getting the data without refreshing the page.
<input type=button value='Get Server Time' onclick="timer_function();">
To this script we will add a timer to recursively call the same Ajax function in every second. This will get data from every second so we can display a changing clock showing server time.
function timer_function(){
var refresh=1000; // Refresh rate in milli seconds
setTimeout(AjaxFunction, refresh);
}
function timer_function(){
var refresh=1000; // Refresh rate in milli seconds
setTimeout(AjaxFunction, refresh);
}
At the end of AjaxFunction() we will call again our timer_function() to make it recursive.
timer_function();
In the main AjaxFunction() we send request to ajax-server-clock-demock.php file and get the server time. This data is displayed using a div layer.
if(httpxml.readyState===4 && httpxml.status===200)
{
document.getElementById("msg").textContent=httpxml.responseText;
document.getElementById("msg").style.background='#f1f1f1';
}
<?php
echo date("d/m/y : H:i:s", time());
?>
You can change the display format of the clock by changing the above code.
<html>
<head>
<title></title>
<script type="text/javascript">
function AjaxFunction()
{
var httpxml = new XMLHttpRequest();
function stateck()
{
if(httpxml.readyState === 4 && httpxml.status === 200)
{
document.getElementById("msg").textContent=httpxml.responseText;
document.getElementById("msg").style.background='#f1f1f1';
}
}
var url="ajax-server-clock-demock.php";
url=url+"?sid="+Math.random();
httpxml.onreadystatechange=stateck;
httpxml.open("GET",url,true);
httpxml.send(null);
timer_function();
}
///////////////////////////
function timer_function(){
var refresh=1000; // Refresh rate in milliseconds
setTimeout(AjaxFunction, refresh);
}
</script>
</head>
<body>
<div id="msg"></div>
<br>
<input type=button value='Get Server Time' onclick="timer_function();">
</body>
</html>
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| ultimate777 | 09-04-2015 |
| Is there a way to do this without the button? So that the time from server is displayed when the page is loaded ? Thanks in advance. | |
| smo | 09-04-2015 |
| Yes, just add onload to body tag, like this <body onload=timer_function();> | |
| melad | 19-04-2015 |
| hi there thx u very much but i have some problem to show javascript in same page not call other url , coz it take some time so now i have this code $now = new DateTime(null, new DateTimeZone('America/New_York')); $now->setTimezone(new DateTimeZone('Europe/London')); echo $now->format("h:i:s A"); how can i replace this code with this var url='ajax-server-clock-demock.php'; and i then can see the DateTimeZone with refresh witout called and url i hobe ur anderstand me thx u | |