-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdynamicServerTime.js
More file actions
80 lines (75 loc) · 2.71 KB
/
dynamicServerTime.js
File metadata and controls
80 lines (75 loc) · 2.71 KB
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
document.write('<script src="https://cdn.bootcss.com/jquery/2.2.3/jquery.min.js"><\/script>');
function dynamicServerTime (options) {
var _this = this;
var _options = {
eleId: '', // 存放最终展示时间的元素id
url: '', // 请求服务器端地址,传入数据为时间戳,秒单位,如不传,则取客户端当前时间
duration: 15000, // 多长时间同步一次服务器端时间,默认15秒
jsonp: true, // 是否jsonp请求方式
dataKey: '', // 如果不为jsonp请求方式,则需提供服务端返回时间戳的key
format: 'y-m-d h:i:s w'
};
_this.timeStr = undefined;
_this.options = Object.assign(_options, options);
_this.ele = document.getElementById (this.options.eleId);
if (!_this.ele) {
throw '未找到id为' + this.options.eleId + '的元素';
}
_this.getTimeStr = function () {
// 清空计时器
clearTimeout(_this.timeout);
// 如果url为空,则取客户端当前时间
if (!_this.options.url) {
_this.timeStr = parseInt(new Date()/1000);
_this.setTime (timeStr);
return;
}
var ajaxObj = {
url: _this.options.url
};
if (_this.options.jsonp) {
ajaxObj.dataType = "jsonp";
ajaxObj.success = function (data) {
_this.timeStr = data;
_this.setTime (_this.timeStr);
}
}else{
ajaxObj.success = function (data) {
_this.timeStr = data[_this.options.dataKey];
_this.setTime (_this.timeStr);
}
}
ajaxObj.error = function (msg) {
console.log (msg)
};
$.ajax (ajaxObj);
};
_this.setTime = function (timeStr) {
let myDate = timeStr ? new Date (timeStr*1000) : new Date ();
let myWeekday = myDate.getDay ();
let myMonth = addZero(myDate.getMonth () + 1);
let myDay = addZero(myDate.getDate ());
let myYear = myDate.getFullYear ();
let myHours = myDate.getHours ();
let myMinutes = addZero(myDate.getMinutes ());
let mySeconds = addZero(myDate.getSeconds ());
let weekday = '星期' + '日一二三四五六'.charAt (myWeekday);
let resultStr = _this.options.format.replace('y', myYear).replace('m', myMonth).replace('d', myDay)
.replace('h', myHours).replace('i', myMinutes).replace('s', mySeconds).replace('w', weekday);
_this.ele.innerText = resultStr;
_this.timeout = setTimeout (function () {
// 每经过1秒钟增加1秒钟的时间戳
_this.timeStr += 1;
_this.setTime (_this.timeStr);
}, 1000);
};
_this.init = function () {
_this.getTimeStr();
// 每隔15秒同步一次服务器端时间
_this.setInterval (getTimeStr, _this.options.duration);
};
_this.init();
function addZero (num) {
return num < 10 ? '0' + num : num;
}
}