Récemment, la page Web du projet doit afficher le temps du serveur en temps réel. Si le temps du serveur est chargé via AJAX par seconde, un grand nombre de demandes seront générées.
Par conséquent, une combinaison de "Javscript Auto-Running Clock" et "Ajax Loading Server Time" a été conçue pour afficher le temps du serveur. "Javscript Auto-Running Clock" s'exécute automatiquement à partir d'une certaine heure initiale comme point de départ, "Ajax Loading Server Time" met à jour le temps du serveur à "Javscript Auto-Running Clock" toutes les 60 secondes.
Horloge d'auto-runscript Javscript:
La copie de code est la suivante:
/ *!
* Fichier: sc_clock.js
* Version: 1.0.0
* Auteur: Lulihong
* Date: 2014-06-06
* Desc: Corloge d'exécution automatiquement
*
* Copyright: open source, utilisez comme vous le souhaitez, veuillez garder la tête.
* /
/ **
* Sortie de format
* @returns
* /
String.prototype.format = function () {
var args = arguments;
return this.replace (// {(/ d +) /} / g, fonction (m, i) {return args [i];});
};
/ **
* Convertir en numéros
* @returns
* /
String.prototype.toint = fonction (defaultv) {
if (this === "" ||! (/ ^ / d + $ /. test (this))) return defaultv;
RETOUR PARSEINT (This);
};
window.scclock =
{
Année: 2014,
mois: 1,
Jour: 1,
heure: 0,
Minute: 0,
Deuxième: 0,
Isrunning: faux,
/ **
* Une fonction qui affiche l'heure, passée par l'appelant lors de l'appel de la fonction de démarrage.
* /
showfunc: function () {},
/ **
* Initialisation
* /
init: fonction (y, lun, d, h, min, s) {
this.year = y;
this.month = lun;
this.day = d;
this.hour = h;
this.minute = min;
this.second = s;
},
/ **
* Temps d'initialisation: Format de temps: 2014-06-09 11:30:30
* /
UpdateTime: function (time) {
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);
},
/ **
* MISE À JOUR TEMPS: Format d'heure: 2014-06-09 11:30:30
* /
UpdateTime: function (time) {
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);
},
/ **
* commencer
* /
Startup: fonction (func) {
if (this.isrunning) retour;
this.isrunning = true;
this.showfunc = func;
window.setTimeout ("ScClock.addonesec ()", 1000);
},
/ **
* Finition
* /
shutdown: function () {
if (! this.isrunning) retour;
this.isrunning = false;
},
/ **
* Obtenez le temps
* /
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.hour <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 fmtsstring.format (this.year, smonth, sday, shour, sminute, ssecond);
},
/ **
* Ajoutez une seconde
* /
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 ++;
}
commutateur (this.month) {
Cas 1:
Cas 3:
Cas 5:
Cas 7:
Cas 8:
Cas 10:
Cas 12: {
if (this.day> 31) {
this.day = 1;
this.month ++;
}
casser;
}
Cas 4:
Cas 6:
Cas 9:
Cas 11: {
if (this.day> 30) {
this.day = 1;
this.month ++;
}
casser;
}
Cas 2: {
if (this.ispeapyear ()) {
if (this.day> 29) {
this.day = 1;
this.month ++;
}
} else if (this.day> 28) {
this.day = 1;
this.month ++;
}
casser;
}
}
if (this.month> 12) {
ce.mombinaire = 1;
this.year ++;
}
this.showfunc (this.getDateTime ());
if (this.isrunning)
window.setTimeout ("ScClock.addonesec ()", 1000);
},
/ **
* La détection de savoir s'il s'agit d'une année de saut: la règle pour juger les années de saut est qu'elle peut être divisée par 4, mais elle peut être divisée par 100, mais elle peut être divisée par 400 pour être une année de saut.
* /
Isleapyear: function () {
if (this.year% 4 == 0) {
if (this.year% 100! = 0) Retourne true;
if (this.year% 400 == 400) renvoie true;
}
retourne false;
}
};
Code d'appel:
La copie de code est la suivante:
/ **
* Démarrer l'heure du système
* /
fonction startupclock () {
if (window.scclock) {
window.scclock.startup (fonction (time) {
$ ("# currtime"). text (heure);
});
}
}
/ **
* Heure du système de chargement
* /
fonction chargystemtime () {
var jSondata = {
"ajaxflag": 1,
"mod": "time_mod"
};
$ .getjson (ajax_sc_url, jsondata, fonction (data) {
if (data.code == 0) {
if (window.scclock)
window.scclock.updatetime (data.time);
}
});
setTimeout ("LoadSystemTime ()", 60000);
}
Code d'affichage HTML:
La copie de code est la suivante:
<span id = "currtime"> </span>