เมื่อเร็ว ๆ นี้หน้าเว็บโครงการจำเป็นต้องแสดงเวลาเซิร์ฟเวอร์แบบเรียลไทม์ หากเวลาเซิร์ฟเวอร์ถูกโหลดผ่าน AJAX ต่อวินาทีจะมีการสร้างคำขอจำนวนมาก
ดังนั้นการรวมกันของ "นาฬิกาที่ดำเนินการด้วยตนเอง Javscript" และ "เวลาเซิร์ฟเวอร์การโหลด AJAX" ได้รับการออกแบบมาเพื่อแสดงเวลาเซิร์ฟเวอร์ "นาฬิกา Javscript ที่ดำเนินการด้วยตนเอง" จะทำงานโดยอัตโนมัติจากเวลาเริ่มต้นเป็นจุดเริ่มต้น "เวลาเซิร์ฟเวอร์การโหลด AJAX" อัพเดตเวลาเซิร์ฟเวอร์เป็น "นาฬิกาที่วิ่งด้วยตนเองของ Javscript" ทุก ๆ 60 วินาที
นาฬิกาที่ใช้งานด้วยตนเองของ Javscript:
การคัดลอกรหัสมีดังนี้:
-
* ไฟล์: sc_clock.js
* เวอร์ชัน: 1.0.0
* ผู้แต่ง: Lulihong
* วันที่: 2014-06-06
* DESC: นาฬิกาทำงานโดยอัตโนมัติ
-
* ลิขสิทธิ์: โอเพ่นซอร์สใช้ตามที่คุณต้องการโปรดรักษาหัวของคุณ
-
-
* รูปแบบเอาต์พุต
* @returns
-
string.prototype.format = function () {
var args = อาร์กิวเมนต์;
ส่งคืนสิ่งนี้ replace (// {(/d+)/}/g, ฟังก์ชัน (m, i) {return args [i];});
-
-
* แปลงเป็นตัวเลข
* @returns
-
string.prototype.toint = function (defaultv) {
if (this === "" ||! (/^/d+$/. ทดสอบ (นี่))) ส่งคืนค่าเริ่มต้น v;
กลับ 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: ฟังก์ชั่น (เวลา) {
var arr = time.split (/[/-//:]/);
if (arr.length! = 6) return;
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: ฟังก์ชั่น (เวลา) {
var arr = time.split (/[/-//:]/);
if (arr.length! = 6) return;
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);
-
-
* เริ่ม
-
Startup: function (func) {
ถ้า (this.isrunning) กลับมา;
this.isrunning = true;
this.showfunc = func;
window.settimeout ("scclock.addonesec ()", 1,000);
-
-
* เสร็จ
-
ปิด: 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 = (นี่คือ <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;
ส่งคืน 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.isliapyear ()) {
if (this.day> 29) {
this.day = 1;
this.month ++;
-
} อื่นถ้า (this.day> 28) {
this.day = 1;
this.month ++;
-
หยุดพัก;
-
-
if (this.month> 12) {
this.month = 1;
this.year ++;
-
this.showfunc (this.getDateTime ());
if (this.isrunning)
window.settimeout ("scclock.addonesec ()", 1,000);
-
-
* การตรวจจับว่าเป็นปีที่ก้าวกระโดด: กฎสำหรับการตัดสินปีกระโดดคือมันสามารถแบ่งออกเป็น 4 แต่มันสามารถแบ่งได้ 100 แต่มันสามารถแบ่ง 400 เป็นปีกระโดด
-
isleapyear: function () {
if (this.year % 4 == 0) {
if (this.year % 100! = 0) กลับมาจริง;
if (this.year % 400 == 400) คืนค่าจริง;
-
กลับเท็จ;
-
-
รหัสโทร:
การคัดลอกรหัสมีดังนี้:
-
* เริ่มเวลาระบบ
-
ฟังก์ชั่น startupClock () {
if (window.scclock) {
window.scclock.startup (ฟังก์ชั่น (เวลา) {
$ ("#currtime") ข้อความ (เวลา);
-
-
-
-
* เวลาในการโหลดระบบ
-
ฟังก์ชั่น loadsystemtime () {
var jSondata = {
"ajaxflag": 1,
"mod": "time_mod"
-
$ .getJson (AJAX_SC_URL, JSONDATA, FUNCTION (DATA) {
if (data.code == 0) {
if (window.scclock)
window.scclock.updateTime (data.time);
-
-
Settimeout ("LoadSystemTime ()", 60000);
-
รหัสการแสดงผล HTML:
การคัดลอกรหัสมีดังนี้:
<span id = "currtime"> </span>