Kürzlich muss die Projektwebseite die Serverzeit in Echtzeit anzeigen. Wenn die Serverzeit über AJAX pro Sekunde geladen wird, werden eine große Anzahl von Anforderungen erstellt.
Daher wurde eine Kombination aus "Javscript Self-Runing Clock" und "Ajax Loading Server Time" so konzipiert, dass die Serverzeit angezeigt wird. "Javscript Self-Running Clock" wird automatisch aus einer bestimmten Anfangszeit als Ausgangspunkt ausgeführt. "AJAX Loading Server Time" aktualisiert die Serverzeit auf "Javscript selbst-Running Clock" alle 60 Sekunden.
Javscript selbst laufende Uhr:
Die Codekopie lautet wie folgt:
/*!
* Datei: sc_clock.js
* Version: 1.0.0
* Autor: Lulihong
* Datum: 2014-06-06
* Desc: Automatisch laufende Uhr
*
* Copyright: Open Source, verwenden Sie, wie Sie möchten, bitte behalten Sie Ihren Kopf.
*/
/**
* Formatausgabe
* @returns
*/
String.prototype.format = function () {
var args = Argumente;
zurückgeben.
};
/**
* Konvertieren Sie zu Zahlen
* @returns
*/
String.prototype.toint = function (defaultv) {
if (this === "" ||! (/^/d+$/. test (this)) return defaultV;
Rückkehr ParseinT (this);
};
window.scclock =
{
Jahr: 2014,
Monat: 1,
Tag: 1,
Stunde: 0,
Minute: 0,
Zweite: 0,
IsRunning: Falsch,
/**
* Eine Funktion, die die Zeit anzeigt, die vom Anrufer beim Aufrufen der Startfunktion übergeben wird.
*/
showfunc: function () {},
/**
* Initialisierung
*/
init: function (y, mon, d, h, min, s) {
this.year = y;
this.month = mon;
this.day = d;
this.bour = h;
this.minute = min;
this.second = s;
},
/**
* Initialisierungszeit: Zeitformat: 2014-06-09 11:30:30
*/
UpdateTime: Funktion (Zeit) {
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);
},
/**
* Update-Zeit: Zeitformat: 2014-06-09 11:30:30
*/
UpdateTime: Funktion (Zeit) {
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);
},
/**
* Start
*/
Startup: Funktion (func) {
wenn (this.isrunning) zurückkehren;
this.isrunning = true;
this.showfunc = func;
window.settimeout ("scclock.addonesec ()", 1000);
},
/**
* Beenden
*/
Herunterfahren: function () {
if (! this.isrunning) kehre zurück;
this.isrunning = false;
},
/**
* Zeit bekommen
*/
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): dieser Tag;
var shour = (this.bour <10)? ("0" + this.bour): this.bour;
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);
},
/**
* Eine Sekunde hinzufügen
*/
AddoneSec: function () {
this.second ++;
if (this.second> = 60) {
this.second = 0;
this.minute ++;
}
if (this.minute> = 60) {
this.minute = 0;
this.bour ++;
}
if (this.hour> = 24) {
this.bour = 0;
this.day ++;
}
Switch (this.month) {
Fall 1:
Fall 3:
Fall 5:
Fall 7:
Fall 8:
Fall 10:
Fall 12: {
if (this.day> 31) {
this.day = 1;
this.month ++;
}
brechen;
}
Fall 4:
Fall 6:
Fall 9:
Fall 11: {
if (this.day> 30) {
this.day = 1;
this.month ++;
}
brechen;
}
Fall 2: {
if (this.isleapyear ()) {
if (this.day> 29) {
this.day = 1;
this.month ++;
}
} else if (this.day> 28) {
this.day = 1;
this.month ++;
}
brechen;
}
}
if (this.onth> 12) {
this.month = 1;
this.year ++;
}
this.showfunc (this.getDatetime ());
if (this.isrunning)
window.settimeout ("scclock.addonesec ()", 1000);
},
/**
* Erkennung, ob es ein Schaltjahr ist: Die Regel für die Beurteilung von Schaltjahren ist, dass es durch 4 geteilt werden kann, aber durch 100 geteilt werden kann, aber es kann durch 400 als Schaltjahr geteilt werden.
*/
isleapyear: function () {
if (this.year % 4 == 0) {
if (this.year % 100! = 0) return true;
if (this.year % 400 == 400) Return True;
}
false zurückgeben;
}
};
Anrufcode:
Die Codekopie lautet wie folgt:
/**
* Die Systemzeit starten
*/
Funktion startupclock () {
if (window.scclock) {
window.scclock.startup (Funktion (Zeit) {
$ ("#currtime"). Text (Zeit);
});
}
}
/**
* Ladesystemzeit
*/
Funktion LoadSystemtime () {
var jSondata = {
"Ajaxflag": 1,,
"mod": "time_mod"
};
$ .getJson (ajax_sc_url, jSondata, Funktion (Daten) {
if (data.code == 0) {
if (window.scclock)
window.scclock.updatetime (Data.Time);
}
});
setTimeout ("lastsystemtime ()", 60000);
}
HTML -Anzeigecode:
Die Codekopie lautet wie folgt:
<span id = "currtime"> </span>