0

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);
});
Inspector Squirrel
  • 2,548
  • 2
  • 27
  • 38
Dharm
  • 87
  • 1
  • 11

6 Answers6

1

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.

Tudor Vintilescu
  • 1,450
  • 2
  • 16
  • 28
0

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

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

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.

Silviu Burcea
  • 5,103
  • 1
  • 29
  • 43
  • thanks Silviu , i do not want client time as it may be wrong . can i do this using jquery? – Dharm Oct 22 '13 at 10:28
  • Yes, you can do it with jQuery, but JS will do as well. All you have to do is to send the server time in your JSP to have the initial server time. – Silviu Burcea Oct 22 '13 at 10:29
0

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.

Ferrmolina
  • 2,737
  • 2
  • 30
  • 46
Abhishek Singh
  • 10,243
  • 22
  • 74
  • 108
0

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>
Barun
  • 1,520
  • 2
  • 12
  • 18
0

Add a label where ever you want to show the server Time

<strong>Server Time&nbsp;:&nbsp;&nbsp;</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.

Baji Shaik
  • 1,022
  • 2
  • 10
  • 14