开发者

Clock in dashboard changes as the system time is changed

开发者 https://www.devze.com 2023-04-01 04:58 出处:网络
My requirement is, the clock in the display should not change wen i change the client time of the system.

My requirement is, the clock in the display should not change wen i change the client time of the system. I have the jboss server..There is the dashboard that should display the server time..i have done some coding but as i change my system timing the clock also changes in the dahsboard..Here goes my code..

from class i am getting the servertime

public Date getServerDateTime() throws Exception {
        Date serverDateTime = null;

        try {
          serverDateTime = new java.util.Date();    

          System.out.println("ServerDateTime-->" +serverDateTime);

        }catch(Exception e ){
            e.printStackTrace();
        }
        return serverDateTime;
    }

In javscript

 var month          =   new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); 
 var weekday        =   new Arra开发者_JAVA百科y("Sun","Mon","Tue","Wed","Thu","Fri");
 //Time Display format -Mon May 16 16:48:04 2011
  //Users time   
  var timeLocal;   
  //Servers time   
  var timeServer;  
  //mill diff between local time and server time
  var  millDiff;      

  //The clock to tick function   
  function toTick(){   
        timeLocal = new Date();   
    //add time difference   
    timeLocal.setMilliseconds(timeLocal.getMilliseconds() - millDiff);   

    //display the value   
    document.getElementById("dateAndTime").innerHTML = month[timeLocal.getMonth()]+" "+(timeLocal.getDate() < 10 ? '0'+timeLocal.getDate() : timeLocal.getDate())+", "+timeLocal.getFullYear()+" "+(timeLocal.getHours() < 10 ? '0'+timeLocal.getHours() : timeLocal.getHours())+":"+(timeLocal.getMinutes() < 10 ? '0'+timeLocal.getMinutes() : timeLocal.getMinutes())+":"+(timeLocal.getSeconds() < 10 ? '0'+timeLocal.getSeconds() : timeLocal.getSeconds());
  }

  /** Display Server date and time in header section ends **/
  /**
   * This function will send a remote call to the server and display the date and time in the
   * gui at the header area
   */
    function getServerDateTime(){
        CommonRemoteCall.getServerDateTime({
         callback:function(datetime) {
           document.getElementById("dateAndTime").innerHTML = month[datetime.getMonth()]+" "+(datetime.getDate() < 10 ? '0'+datetime.getDate() : datetime.getDate())+", "+datetime.getFullYear()+" "+(datetime.getHours() < 10 ? '0'+datetime.getHours() : datetime.getHours())+":"+(datetime.getMinutes() < 10 ? '0'+datetime.getMinutes() : datetime.getMinutes())+":"+(datetime.getSeconds() < 10 ? '0'+datetime.getSeconds() : datetime.getSeconds());
             //Server time 
             timeServer = datetime;

               //Users time 
             timeLocal = new Date();

             //Calculate the difference (returns milliseconds)   
             millDiff = timeLocal - timeServer;


         } 
      });
  }

Please help..where Am i goin wrong, Is ter any better way of doin it???

How to prevent the dashbord from using terminal clock and use only the server clock


If you base it off the client clock, it will break when the user changes the clock since the reference point changes.

How can you detect when the user changes it?

Keep track of the previous time when you update the display. If the previous time differs significantly, you can readjust by the millDiff.

Note: The time is NEVER going to be exact because of the time it takes for the request to travel across the network and the time it takes the page to be rendered on the client.


As long as you are using any code like

new Date(); //or
Date.now(); //if you like milliseconds

you need some way of updating the time difference between the server and the client. One solution would be to use window.setInterval on, say, a 1000ms delay and see whether the client time has changed by an amount significantly different from 1000ms. If it has, you need to re-sync with the server.

E.g.,

var lastTimeLocal = null;
var interval = 1000;
var fudge = 0.05;
window.setInterval(function() {
    var thisTime = Date.now();
    if(lastTimeLocal == null)
      return;
    var delta = thisTime - lastTimeLocal;
    lastTimeLocal = thisTime;
    if(delta < interval * (1.0 - fudge) || delta > interval * (1.0 + fudge))
      getServerDateTime();
}, interval);

It would probably be best to fold this into your toTick function, but you don't need to.

0

精彩评论

暂无评论...
验证码 换一张
取 消