-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.remoteclock.js
135 lines (112 loc) · 3.77 KB
/
jquery.remoteclock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*!
* jQuery Remote Clock 1.0.0
*
* Author: Andrii Volin <[email protected]>
* Released under the MIT license
* https://github.com/angyvolin/jquery-remote-clock/blob/master/LICENSE
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof exports === 'object') {
// Node/CommonJS style for Browserify
module.exports = factory;
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
var old;
var RemoteClock = function (element, options) {
this.type = null;
this.options = null;
this.tickTimeout = null;
this.syncTimeout = null;
this.diff = 0;
this.$element = null;
this.$clock = null;
this.parser = null;
this.init('remoteClock', element, options);
};
RemoteClock.VERSION = '1.0.0';
RemoteClock.TICK_INTERVAL = 1000;
RemoteClock.DEFAULTS = {
url: '/time',
syncInterval: 10000,
template: '<span class="time">88:88:88</span>',
selector: '.time',
parser: Date.parse
};
RemoteClock.prototype.init = function (type, element, options) {
this.type = type;
this.options = $.extend({}, RemoteClock.DEFAULTS, options || {});
this.$element = $(element).append(this.options.template);
this.$clock = this.$element.find(this.options.selector);
this.setParser(this.options.parser);
this.sync();
};
RemoteClock.prototype.setParser = function (parser) {
if ('object' === typeof parser) {
this.parser = parser.parse;
} else if ('function' === typeof parser) {
this.parser = parser;
} else {
throw Error('The "parser" option is expected to be of type function or object with ::parse() method.');
}
};
RemoteClock.prototype.sync = function () {
var self = this;
clearTimeout(this.syncTimeout);
$.ajax({
url: this.options.url,
suppressErrors: true
}).done(function (serverTime) {
self.diff = self.parser(serverTime) - Date.now();
self.tick.apply(self);
self.syncTimeout = setTimeout($.proxy(self.sync, self), self.options.syncInterval);
});
};
RemoteClock.prototype.tick = function () {
clearTimeout(this.tickTimeout);
this.$clock.text(
new Date(Date.now() + this.diff).toTimeString().split(' ')[0]
);
this.tickTimeout = setTimeout($.proxy(this.tick, this), RemoteClock.TICK_INTERVAL);
};
RemoteClock.prototype.destroy = function () {
clearInterval(this.tickTimeout);
clearInterval(this.syncTimeout);
this.$element.off('click').removeData(this.type);
if (null !== this.$clock) {
this.$clock.detach();
this.$clock = null;
}
this.$element.empty();
this.$element = null;
};
function Plugin(option) {
return this.each(function () {
var $this = $(this);
var data = $this.data('remoteClock');
var options = 'object' === typeof option && option;
if (!data && 'destroy' === option) {
return;
}
if (!data) {
data = new RemoteClock(this, options);
$this.data('remoteClock', data);
}
if ('string' === typeof option) {
data[option]();
}
});
}
old = $.fn.remoteClock;
$.fn.remoteClock = Plugin;
$.fn.remoteClock.Constructor = RemoteClock;
$.fn.remoteClock.noConflict = function () {
$.fn.remoteClock = old;
return this;
};
}));