في الآونة الأخيرة ، تحتاج صفحة ويب المشروع إلى عرض وقت الخادم في الوقت الفعلي. إذا تم تحميل وقت الخادم من خلال AJAX في الثانية ، فسيتم إنشاء عدد كبير من الطلبات.
لذلك ، تم تصميم مزيج من "ساعة Javscript ذاتية التشغيل" و "Ajax Loading Server Time" لعرض وقت الخادم. "ساعة Javscript ذاتية التشغيل" تعمل تلقائيًا من وقت أولي معين كنقطة انطلاق ، "يقوم Ajax Loading Server Time" بتحديث وقت الخادم إلى "ساعة Javscript ذاتية التشغيل" كل 60 ثانية.
ساعة Javscript الذاتية:
نسخة الكود كما يلي:
/*!
* ملف: sc_clock.js
* الإصدار: 1.0.0
* المؤلف: لوليهونغ
* التاريخ: 2014-06-06
* DESC: تشغيل الساعة تلقائيًا
*
* حقوق الطبع والنشر: المصدر المفتوح ، استخدم كما تريد ، يرجى الاحتفاظ برأسك.
*/
/**
* تنسيق الإخراج
* @العودة
*/
string.prototype.format = function () {
var args = الوسيطات ؛
إرجاع this.replace (// {(/d+)/}/g ، function (m ، i) {return args [i] ؛}) ؛
} ؛
/**
* تحويل إلى أرقام
* @العودة
*/
string.prototype.toint = function (defaultv) {
if (هذا === "" ||! (/^/d+$/. test (this))) return defaultv ؛
إرجاع parseint (هذا) ؛
} ؛
window.scclock =
{
السنة: 2014 ،
الشهر: 1 ،
اليوم: 1 ،
الساعة: 0 ،
الدقيقة: 0 ،
ثانياً: 0 ،
isrunning: خطأ ،
/**
* وظيفة تعرض الوقت ، تم تمريرها بواسطة المتصل عند الاتصال بوظيفة بدء التشغيل.
*/
showfunc: function () {} ،
/**
* التهيئة
*/
init: function (y ، mon ، d ، h ، min ، s) {
this.year = y ؛
this.month = mon ؛
this.day = d ؛
this.hour = h ؛
this.minute = min ؛
this.Second = s ؛
} ،
/**
* وقت التهيئة: تنسيق الوقت: 2014-06-09 11:30:30
*/
updateTime: function (time) {
var arr = time.split (/[/-//:]/) ؛
إذا (arr.length! = 6) العودة ؛
this.year = arr [0] .toint (2014) ؛
this.month = arr [1] .Toint (1) ؛
this.day = arr [2] .toint (1) ؛
this.hour = arr [3] .Toint (0) ؛
this.minute = arr [4] .toint (0) ؛
this.second = arr [5] .toint (0) ؛
} ،
/**
* وقت التحديث: تنسيق الوقت: 2014-06-09 11:30:30
*/
updateTime: function (time) {
var arr = time.split (/[/-//:]/) ؛
إذا (arr.length! = 6) العودة ؛
this.year = arr [0] .toint (2014) ؛
this.month = arr [1] .Toint (1) ؛
this.day = arr [2] .toint (1) ؛
this.hour = arr [3] .Toint (0) ؛
this.minute = arr [4] .toint (0) ؛
this.second = arr [5] .toint (0) ؛
} ،
/**
* يبدأ
*/
بدء التشغيل: وظيفة (func) {
إذا (this.isrunning) العودة ؛
this.isrunning = صحيح ؛
this.showfunc = func ؛
Window.SettImeout ("scclock.addonesec ()" ، 1000) ؛
} ،
/**
* ينهي
*/
إيقاف التشغيل: Function () {
إذا (! this.isrunning) العودة ؛
this.isrunning = false ؛
} ،
/**
* احصل على الوقت
*/
getDateTime: function () {
var fmtstring = "{0}-{1}-{2} {3}: {4}: {5}" ؛
var smonth = (this.month <10)؟ ("0" + this.month): this.month ؛
var sday = (this.day <10)؟ ("0" + this.day): this.day ؛
var shour = (this.hor <10)؟ ("0" + this.hour): this.hour ؛
var sminute = (this.minute <10)؟ ("0" + this.minute): this.minute ؛
var ssecond = (this.Second <10)؟ ("0" + this.second): this.second ؛
return fmtstring.format (this.year ، smonth ، sday ، shour ، sminute ، ssecond) ؛
} ،
/**
* أضف ثانية واحدة
*/
addonesec: function () {
this.Second ++ ؛
if (this.second> = 60) {
this.Second = 0 ؛
this.minute ++ ؛
}
if (this.minute> = 60) {
this.minute = 0 ؛
this.hour ++ ؛
}
if (this.hour> = 24) {
this.hour = 0 ؛
this.day ++ ؛
}
التبديل (this.month) {
الحالة 1:
الحالة 3:
الحالة 5:
الحالة 7:
الحالة 8:
الحالة 10:
الحالة 12: {
if (this.day> 31) {
this.day = 1 ؛
this.month ++ ؛
}
استراحة؛
}
الحالة 4:
الحالة 6:
الحالة 9:
الحالة 11: {
if (this.day> 30) {
this.day = 1 ؛
this.month ++ ؛
}
استراحة؛
}
الحالة 2: {
if (this.isleapyear ()) {
if (this.day> 29) {
this.day = 1 ؛
this.month ++ ؛
}
} آخر إذا (this.day> 28) {
this.day = 1 ؛
this.month ++ ؛
}
استراحة؛
}
}
if (this.mon> 12) {
this.month = 1 ؛
this.year ++ ؛
}
this.showfunc (this.getDateTime ()) ؛
إذا (this.isrunning)
Window.SettImeout ("scclock.addonesec ()" ، 1000) ؛
} ،
/**
* اكتشاف ما إذا كانت سنة قفزة: إن قاعدة الحكم على سنوات القفز هي أنه يمكن تقسيمها على 4 ، ولكن يمكن تقسيمها على 100 ، ولكن يمكن تقسيمها على 400 عام لتكون سنة قفزة.
*/
isLeapyear: function () {
if (this.year ٪ 4 == 0) {
if (this.ear ٪ 100! = 0) return true ؛
if (this.ear ٪ 400 == 400) return true ؛
}
العودة كاذبة
}
} ؛
رمز الاتصال:
نسخة الكود كما يلي:
/**
* بدء وقت النظام
*/
وظيفة startupClock () {
if (window.scclock) {
window.scclock.startup (وظيفة (الوقت) {
$ ("#curtime"). النص (الوقت) ؛
}) ؛
}
}
/**
* وقت التحميل
*/
وظيفة loadsystemtime () {
var jsondata = {
"Ajaxflag": 1 ،
"وزارة الدفاع": "time_mod"
} ؛
$ .getjson (ajax_sc_url ، jsondata ، function (data) {
if (data.code == 0) {
إذا (window.scclock)
window.scclock.updateTime (data.time) ؛
}
}) ؛
setTimeout ("loadsystemtime ()" ، 60000) ؛
}
رمز عرض HTML:
نسخة الكود كما يلي:
<span id = "currtime"> </span>