Skip to content

Commit 03d6f2f

Browse files
committed
update syncedclock so it syncs faster
1 parent 12c2431 commit 03d6f2f

File tree

1 file changed

+40
-34
lines changed

1 file changed

+40
-34
lines changed

public/hft/0.x.x/scripts/syncedclock.js

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -112,43 +112,49 @@ define(["./io"], function(IO) {
112112
* keep the clock synced to the server.
113113
* @constructor
114114
*/
115-
var SyncedClock = function(opt_syncRateSeconds, opt_callback) {
116-
this.url = window.location.href;
117-
this.syncRateMS = (opt_syncRateSeconds || 10) * 1000;
118-
this.timeOffset = 0;
119-
this.callback = opt_callback;
120-
this.syncToServer();
121-
};
115+
var SyncedClock = function(opt_syncRateSeconds, callback) {
116+
var url = window.location.href;
117+
var syncRateMS = (opt_syncRateSeconds || 10) * 1000;
118+
var timeOffset = 0;
119+
var callback = opt_callback;
120+
121+
var syncToServer = function(queueNext) {
122+
var sendTime = getLocalTime();
123+
IO.sendJSON(url, {cmd: 'time'}, function(exception, obj) {
124+
if (exception) {
125+
//g_services.logger.error("syncToServer: " + exception);
126+
} else {
127+
var receiveTime = getLocalTime();
128+
var duration = receiveTime - sendTime;
129+
var serverTime = obj.time + duration * 0.5;
130+
timeOffset = serverTime - receiveTime;
131+
if (callback) {
132+
callback();
133+
callback = undefined;
134+
}
135+
//g_services.logger.log("duration: ", duration, " timeOff:", timeOffset);
136+
}
122137

123-
SyncedClock.prototype.syncToServer = function() {
124-
var that = this;
125-
var sendTime = getLocalTime();
126-
IO.sendJSON(this.url, {cmd: 'time'}, function(exception, obj) {
127-
if (exception) {
128-
//g_services.logger.error("syncToServer: " + exception);
129-
} else {
130-
var receiveTime = getLocalTime();
131-
var duration = receiveTime - sendTime;
132-
var serverTime = obj.time + duration * 0.5;
133-
that.timeOffset = serverTime - receiveTime;
134-
if (that.callback) {
135-
that.callback();
136-
that.callback = undefined;
138+
if (queueNext) {
139+
setTimeout(function() {
140+
syncToServer(true);
141+
}, syncRateMS);
137142
}
138-
//g_services.logger.log("duration: ", duration, " timeOff:", that.timeOffset);
139-
}
140-
setTimeout(function() {
141-
that.syncToServer();
142-
}, that.syncRateMS);
143-
});
144-
};
143+
});
144+
};
145+
syncToServer(true);
146+
setTimeout(function() {syncToServer(false)}, 1000);
147+
setTimeout(function() {syncToServer(false)}, 2000);
148+
setTimeout(function() {syncToServer(false)}, 4000);
149+
150+
/**
151+
* Gets the current time in seconds.
152+
* @private
153+
*/
154+
this.getTime = function() {
155+
return getLocalTime() + timeOffset;
156+
};
145157

146-
/**
147-
* Gets the current time in seconds.
148-
* @private
149-
*/
150-
SyncedClock.prototype.getTime = function() {
151-
return getLocalTime() + this.timeOffset;
152158
};
153159

154160
return online ? new SyncedClock(opt_syncRateSeconds, opt_callback) : new LocalClock(opt_callback);

0 commit comments

Comments
 (0)