Recentemente, a página da web do projeto precisa exibir o tempo do servidor em tempo real. Se o tempo do servidor for carregado através do AJAX por segundo, um grande número de solicitações será gerado.
Portanto, uma combinação do relógio "Javscript Autonning Clock" e "Ajax Loading Server" foi projetado para exibir o horário do servidor. O "Relógio Javscript Auto-Running" é executado automaticamente a partir de um determinado horário inicial como ponto de partida ", a hora do servidor de carregamento do Ajax" atualiza o tempo do servidor para "Relógio de auto-execução Javscript" a cada 60 segundos.
Relógio de auto-execução Javscript:
A cópia do código é a seguinte:
/*!
* Arquivo: sc_clock.js
* Versão: 1.0.0
* Autor: Lulihong
* Data: 2014-06-06
* DEC: Relógio de execução automaticamente
*
* Copyright: Open Source, Use como você quiser, mantenha sua cabeça.
*/
/**
* Saída de formato
* @RETURNS
*/
String.prototype.format = function () {
var args = argumentos;
Retorne this.replace (// {(/d+)/}/g, function (m, i) {return args [i];});
};
/**
* Converter em números
* @RETURNS
*/
String.prototype.toint = function (defaultv) {
if (this === "" ||! (/^/d+$/. test (this))) retorne defaultv;
retornar parseint (isto);
};
window.scclock =
{
Ano: 2014,
mês: 1,
Dia: 1,
hora: 0,
minuto: 0,
Segundo: 0,
isrunning: false,
/**
* Uma função que exibe o tempo, passada pelo chamador ao chamar a função de inicialização.
*/
showfunc: function () {},
/**
* Inicialização
*/
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;
},
/**
* Horário de inicialização: Formato de tempo: 2014-06-09 11:30:30
*/
UpdateTime: function (tempo) {
var arr = time.split (/[/-//:]/);
if (arr.length! = 6) retornar;
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);
},
/**
* Hora da atualização: Time Format: 2014-06-09 11:30:30
*/
UpdateTime: function (tempo) {
var arr = time.split (/[/-//:]/);
if (arr.length! = 6) retornar;
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);
},
/**
* começar
*/
Startup: function (func) {
if (this.isrunning) retornar;
this.isrunning = true;
this.showfunc = func;
window.setTimeout ("scclock.addonesec ()", 1000);
},
/**
* Terminar
*/
desligamento: function () {
if (! this.isrunning) retornar;
this.isrunning = false;
},
/**
* Obtenha tempo
*/
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 fmtstring.format (this.year, smonth, sday, shour, sminute, ssecond);
},
/**
* Adicione um segundo
*/
addoneec: 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 ++;
}
quebrar;
}
Caso 4:
Caso 6:
Caso 9:
Caso 11: {
if (this.day> 30) {
this.day = 1;
this.month ++;
}
quebrar;
}
Caso 2: {
if (this.isleapyear ()) {
if (this.day> 29) {
this.day = 1;
this.month ++;
}
} else if (this.day> 28) {
this.day = 1;
this.month ++;
}
quebrar;
}
}
if (this.month> 12) {
this.month = 1;
this.year ++;
}
this.showfunc (this.getDateTime ());
if (this.isrunning)
window.setTimeout ("scclock.addonesec ()", 1000);
},
/**
* Detecção de se é um ano bissexto: a regra para julgar anos bissextos é que ela pode ser dividida por 4, mas pode ser dividida por 100, mas pode ser dividida por 400 para ser um ano bissexto.
*/
isleapyear: function () {
if (this.year % 4 == 0) {
if (this.year % 100! = 0) retorne true;
if (this.year % 400 == 400) retornar TRUE;
}
retornar falso;
}
};
Código de chamada:
A cópia do código é a seguinte:
/**
* Iniciar o tempo do sistema
*/
função startupclock () {
if (window.scclock) {
window.scclock.startup (function (tempo) {
$ ("#currtime"). Texto (tempo);
});
}
}
/**
* Hora do sistema de carregamento
*/
função loadSystemTime () {
var jsondata = {
"Ajaxflag": 1,
"Mod": "time_mod"
};
$ .getjson (ajax_sc_url, jsondata, function (dados) {
if (data.code == 0) {
if (window.scclock)
window.scclock.updateTime (data.time);
}
});
setTimeout ("loadSystemTime ()", 60000);
}
Código de exibição HTML:
A cópia do código é a seguinte:
<span id = "currtime"> </span>