Recientemente, la página web del proyecto debe mostrar el tiempo del servidor en tiempo real. Si el tiempo del servidor se carga a través de AJAX por segundo, se generará una gran cantidad de solicitudes.
Por lo tanto, se diseñó una combinación de "Javscript Self-Running Clock" y "Ajax Loading Server Time" para mostrar el tiempo del servidor. "Javscript Self-Running Clock" se ejecuta automáticamente desde una determinada hora inicial como punto de partida, "AJAX Carging Servidor Time" actualiza el tiempo del servidor a "Javscript Self-Running Clock" cada 60 segundos.
Javscript Self-Running Clock:
La copia del código es la siguiente:
/*!
* Archivo: sc_clock.js
* Versión: 1.0.0
* Autor: Lulihong
* Fecha: 2014-06-06
* DESC: Reloj funcionando automáticamente
*
* Copyright: código abierto, use como desee, por favor mantenga la cabeza.
*/
/**
* Salida de formato
* @returns
*/
String.prototype.format = function () {
var args = argumentos;
return this.replace (// {(/d+)/}/g, función (m, i) {return args [i];});
};
/**
* Convertir a números
* @returns
*/
String.prototype.toint = function (defaultv) {
if (this === "" ||! (/^/d+$/. test (this))) devuelve defaultv;
devolver parseint (esto);
};
Window.ScClock =
{
Año: 2014,
Mes: 1,
Día: 1,
Hora: 0,
minuto: 0,
Segundo: 0,
isrunning: falso,
/**
* Una función que muestra el tiempo, pasada por la persona que llama al llamar a la función de inicio.
*/
showFunc: function () {},
/**
* Inicialización
*/
Init: Función (Y, Mon, D, H, Min, S) {
this.Year = y;
this.Month = Mon;
this.day = D;
this.hour = h;
this.minute = min;
this.second = s;
},
/**
* Tiempo de inicialización: Formato de tiempo: 2014-06-09 11:30:30
*/
UpdateTime: Function (Time) {
var arrr = 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);
},
/**
* Tiempo de actualización: Formato de tiempo: 2014-06-09 11:30:30
*/
UpdateTime: Function (Time) {
var arrr = 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);
},
/**
* comenzar
*/
startup: function (func) {
if (this.isrunning) regreso;
this.isrunning = true;
this.showfunc = func;
Window.setTimeout ("scClock.addonesec ()", 1000);
},
/**
* Finalizar
*/
apagado: function () {
if (! this.isrunning) regreso;
this.isrunning = false;
},
/**
* Obtener tiempo
*/
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.hur;
var sminute = (this.minute <10)? ("0" + this.minute): this.minute;
var ssecond = (this.segund <10)? ("0" + this.second): this.second;
return fmtString.Format (this.year, smonth, sday, shour, sminute, ssecond);
},
/**
* Agregar un segundo
*/
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 ++;
}
Switch (this.Month) {
Caso 1:
Caso 3:
Caso 5:
Caso 7:
Caso 8:
Caso 10:
Caso 12: {
if (this.day> 31) {
this.day = 1;
this.Month ++;
}
romper;
}
Caso 4:
Caso 6:
Caso 9:
Caso 11: {
if (this.day> 30) {
this.day = 1;
this.Month ++;
}
romper;
}
Caso 2: {
if (this.eSeSeapyear ()) {
if (this.day> 29) {
this.day = 1;
this.Month ++;
}
} else if (this.day> 28) {
this.day = 1;
this.Month ++;
}
romper;
}
}
if (this.Month> 12) {
this.Month = 1;
this. años ++;
}
this.showfunc (this.getDateTime ());
if (this.srunning)
Window.setTimeout ("scClock.addonesec ()", 1000);
},
/**
* Detección de si es un año bisiesto: la regla para juzgar los años de salto es que se puede dividir por 4, pero puede dividirse por 100, pero puede dividirse por 400 como un año bisiesto.
*/
isleapyear: function () {
if (this.Year % 4 == 0) {
if (this.Year % 100! = 0) devuelve verdadero;
if (this.Year % 400 == 400) devuelve verdadero;
}
devolver falso;
}
};
Código de llamada:
La copia del código es la siguiente:
/**
* Iniciar tiempo del sistema
*/
función startupClock () {
if (window.scclock) {
window.scclock.startup (function (time) {
$ ("#Currtime"). Text (tiempo);
});
}
}
/**
* Tiempo del sistema de carga
*/
function loadSystemTime () {
var jsondata = {
"Ajaxflag": 1,
"Mod": "Time_mod"
};
$ .getjson (AJAX_SC_URL, JSONDATA, FUNTA (DATA) {
if (data.code == 0) {
if (window.scclock)
window.scclock.updateTime (data.time);
}
});
setTimeOut ("LoadSystemTime ()", 60000);
}
Código de visualización HTML:
La copia del código es la siguiente:
<span id = "currtime"> </span>