diff options
Diffstat (limited to 'www/js/clock.js')
-rw-r--r-- | www/js/clock.js | 24 |
1 files changed, 9 insertions, 15 deletions
diff --git a/www/js/clock.js b/www/js/clock.js index 96ec9e4..3562b1e 100644 --- a/www/js/clock.js +++ b/www/js/clock.js @@ -44,11 +44,11 @@ function Clock() { time_str += (rdtime.getUTCSeconds() > 9 ? ':' : ':0') + rdtime.getUTCSeconds(); this.draw_callbacks.fireWith(window, [date_str, time_str, get_rd_week(rdtime_ms)]); - } + }; this.addCallback = function(cb) { this.draw_callbacks.add(cb); - } + }; this.ntp_update = function(event) { var t4 = (+new Date()); @@ -59,18 +59,18 @@ function Clock() { this.clock_offset = ((msg.t2 - msg.t1) + (msg.t3 - msg.t4)) / 2; this.clock_rtt = (msg.t4 - msg.t1) - (msg.t3 - msg.t2); // console.log('got new ntp message from rhrdtime (rtt=' + this.clock_rtt + ' ms): new offset = ' + this.clock_offset + ' ms'); - } + }; this.ntp_request = function() { this.sock.send(JSON.stringify({ t1: (+new Date()), t2: 0, t3: 0, t4: 0, tz_offset: 0, week: 0 })); - } + }; this.sock_onopen = function() { // console.log('clock websocket connection established'); this.state = 'CONNECTED'; this.ntp_request(); this.interval_request = setInterval(this.ntp_request.bind(this), 2000); - } + }; this.sock_onclose = function(event) { if(this.state == 'STOPPED') { @@ -84,7 +84,7 @@ function Clock() { setTimeout(this.connect.bind(this), 1000); this.state = 'RECONNECTING'; } - } + }; this.connect = function() { this.sock = new WebSocket('wss://' + window.location.host + '/ntp'); @@ -92,12 +92,12 @@ function Clock() { this.sock.onopen = this.sock_onopen.bind(this); this.sock.onclose = this.sock_onclose.bind(this); this.state = 'CONNECTING'; - } + }; this.start = function() { this.connect(); this.interval_redraw = setInterval(this.redraw.bind(this), 200); - } + }; this.stop = function() { this.state = 'STOPPED'; @@ -106,13 +106,7 @@ function Clock() { clearInterval(this.interval_request); delete this.interval_request; this.sock.close(); - } -} - -var clock = new Clock(); - -function clock_init() { - clock.start(); + }; } function clock_add_callback(cb) { |