src/hft/scripts/syncedclock.js
function(opt_syncRateSeconds, callback) {
      var query = misc.parseUrlQuery();
      var url = (query.hftUrl || window.location.href).replace("ws:", "http:");

      var syncRateMS = (opt_syncRateSeconds || 10) * 1000;
      var timeOffset = 0;

      var syncToServer = function(queueNext) {
        var sendTime = getLocalTime();
        io.sendJSON(url, {cmd: 'time'}, function(exception, obj) {
          if (exception) {
            console.error("syncToServer: " + exception);
          } else {
            var receiveTime = getLocalTime();
            var duration = receiveTime - sendTime;
            var serverTime = obj.time + duration * 0.5;
            timeOffset = serverTime - receiveTime;
            if (callback) {
              callback();
              callback = undefined;
            }
            //g_services.logger.log("duration: ", duration, " timeOff:", timeOffset);
          }

          if (queueNext) {
            setTimeout(function() {
              syncToServer(true);
            }, syncRateMS);
          }
        });
      };
      var syncToServerNoQueue = function() {
        syncToServer(false);
      };
      syncToServer(true);
      setTimeout(syncToServerNoQueue, 1000);
      setTimeout(syncToServerNoQueue, 2000);
      setTimeout(syncToServerNoQueue, 4000);

      /**
       * Gets the current time in seconds.
       * @private
       */
      this.getTime = function() {
        return getLocalTime() + timeOffset;
      };

    }
function(opt_syncRateSeconds, callback) {
      var query = misc.parseUrlQuery();
      var url = (query.hftUrl || window.location.href).replace("ws:", "http:");

      var syncRateMS = (opt_syncRateSeconds || 10) * 1000;
      var timeOffset = 0;

      var syncToServer = function(queueNext) {
        var sendTime = getLocalTime();
        io.sendJSON(url, {cmd: 'time'}, function(exception, obj) {
          if (exception) {
            console.error("syncToServer: " + exception);
          } else {
            var receiveTime = getLocalTime();
            var duration = receiveTime - sendTime;
            var serverTime = obj.time + duration * 0.5;
            timeOffset = serverTime - receiveTime;
            if (callback) {
              callback();
              callback = undefined;
            }
            //g_services.logger.log("duration: ", duration, " timeOff:", timeOffset);
          }

          if (queueNext) {
            setTimeout(function() {
              syncToServer(true);
            }, syncRateMS);
          }
        });
      };
      var syncToServerNoQueue = function() {
        syncToServer(false);
      };
      syncToServer(true);
      setTimeout(syncToServerNoQueue, 1000);
      setTimeout(syncToServerNoQueue, 2000);
      setTimeout(syncToServerNoQueue, 4000);

      /**
       * Gets the current time in seconds.
       * @private
       */
      this.getTime = function() {
        return getLocalTime() + timeOffset;
      };

    }
function ( opt_syncRateSeconds , callback ) { var query = misc . parseUrlQuery ( ) ; var url = ( query . hftUrl || window . location . href ) . replace ( "ws:" , "http:" ) ; var syncRateMS = ( opt_syncRateSeconds || 10 ) * 1000 ; var timeOffset = 0 ; var syncToServer = function ( queueNext ) { var sendTime = getLocalTime ( ) ; io . sendJSON ( url , { cmd : 'time' } , function ( exception , obj ) { if ( exception ) { console . error ( "syncToServer: " + exception ) ; } else { var receiveTime = getLocalTime ( ) ; var duration = receiveTime - sendTime ; var serverTime = obj . time + duration * 0.5 ; timeOffset = serverTime - receiveTime ; if ( callback ) { callback ( ) ; callback = undefined ; } //g_services.logger.log("duration: ", duration, " timeOff:", timeOffset); } if ( queueNext ) { setTimeout ( function ( ) { syncToServer ( true ) ; } , syncRateMS ) ; } } ) ; } ; var syncToServerNoQueue = function ( ) { syncToServer ( false ) ; } ; syncToServer ( true ) ; setTimeout ( syncToServerNoQueue , 1000 ) ; setTimeout ( syncToServerNoQueue , 2000 ) ; setTimeout ( syncToServerNoQueue , 4000 ) ; /**
       * Gets the current time in seconds.
       * @private
       */ this . getTime = function ( ) { return getLocalTime ( ) + timeOffset ; } ; }
A clock that gets the current time in seconds attempting to
keep the clock synced to the server.
@constructor
A clock that gets the current time in seconds attempting to keep the clock synced to the server .