Reputation: 87
I am displaying server time
on a jsp
page, and in order to display correct time, I have to call that jsp
page to refresh the displayed time which leads to many requests
to the server
.
Can anyone suggest any improvement on the way I'm displaying server time on jsp
page without sending request
frequently?
function displayserver(){
$.post("DisplayServerTime.jsp","",function(data,status, req){
$("#DisplayTimeSection").text(req.responseText.trim());
});
}
$(function(){
setInterval(
function(){
displayserver();
}
, 30000);
});
Upvotes: 0
Views: 5878
Reputation: 9729
Step 1 : Get the server Date object on your page using request.getAttribute("serverDate");
or session.getAttribute("serverDate");
Step 2 : Create a javascript Object from the date recieved in step 1
Step 3 : Add a client-side setInterval
it will increment Step2 object by 1 second and set the time back in the page.
Upvotes: 0
Reputation: 1122
Add a label where ever you want to show the server Time
<strong>Server Time : </strong><label id="timelable"></label>
And then add the following java script code at the end of the jsp inside the body tag
<script type="text/javascript">
var myVar = setInterval(function(){ myTimer() }, 1000);
var jsVar= <%=java.util.Calendar.getInstance().getTimeInMillis()%>;
var timeZoneOffset=<%=java.util.TimeZone.getDefault().getOffset(System.currentTimeMillis())%>;
jsVar=jsVar+timeZoneOffset;
function myTimer() {
jsVar=jsVar+1000;
var d = new Date(jsVar);
var t=d.toUTCString();
document.getElementById("timelable").innerHTML = t;
}
</script>
Thats it now you will see the server time running in you jsp.
Upvotes: 0
Reputation: 1622
The idea here is to call the time.jsp once every five times when the displayserver method is called. For the other 4 times when the time.jsp is not called, we update a javascript Date object and display it.
<script type="text/javascript">
var jsVar=0;
var d = new Date();
var timeInterval = 3000;
var doPost=true;
var postCount=0;
function displayserver(){
if(doPost && postCount == 0){
$.post("time.jsp","")
.done(function(msg){
jsVar = msg;
doPost = false;
});
}else{
postCount++;
jsVar = d.getTime() + timeInterval;
if(postCount == 4){
postCount=0;
doPost = true;
}
}
d.setTime(jsVar);
$("#DisplayTimeSection").text(d);
}
$(function(){
setInterval(
function(){
displayserver();
}
, timeInterval);
});
</script>
Upvotes: 0
Reputation: 121998
without sending request frequently.
While loading the page get the server time in long and create a Date(millis)
Object with Javascript
.
Then use setInterval()
with 1 sec and display.
In you Jsp
var jsVar= <% dateMillis %>; // server millis
and javascript date formation
var d = new Date();
d.setTime(jsVar); // server millis ex :1332403882588
Then show in JS
var x = document.getElementById("timelable");
x.innerHTML=d.getMilliseconds(); // Modify as per your format
Then
window.setInterval(Yourfunction,milliseconds);
Then in your Yourfunction
add 1 sec to your millis and refresh lable
Upvotes: 0
Reputation: 1460
Get the server time when you first load the page, then find out the time difference between the client's time and the server's time.
Second, run recurrently a function (using setInterval()). On each invocation get the client time and add the time difference you got at the first step, then display it.
You'll need to run the interval function something like twice per second to be sure you don't skip seconds.
Upvotes: 1
Reputation: 5348
This can be done easily. Just send to the client the server time
and add a client-side setInterval
that will get the time, increment by 1 second and set the time back in the page.
Note that if you want the client time
, you can get the time as easy as new Date()
in JS and apply the setInterval
too.
Upvotes: 0