客戶端本地時間不可靠,服務器時間權威但需解決網絡延遲、頁面緩存、跨域盜用等問題。本文提供生產級解決方案:ASP 注入服務器時間基準,JS 客戶端高精度走時,農曆顯示,防緩存/防盜鏈雙重保障。
t0,計算偏差 dectime = ServerTime - t0,後續走時基於 localTime + dectime。setInterval(Draw, 500) 每 500ms 更新 DOM,視覺流暢。<%@ Language=VBScript CodePage=65001 %><% Response.Charset = "UTF-8" %>
<%
' === 防盜鏈:檢查 Referer ===
Dim referer, allowedDomain
referer = Request.ServerVariables("HTTP_REFERER")
allowedDomain = Request.ServerVariables("SERVER_NAME")
If referer = "" Or InStr(1, referer, allowedDomain, 1) = 0 Then
Response.Status = "403 Forbidden"
Response.Write "非法調用"
Response.End
End If
' === 防緩存:強制無緩存 ===
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1
' === 輸出服務器時間分量 ===
Dim d, t
d = Date()
t = Time()
%>
<script>
// === 時間同步核心 ===
var t0 = new Date().getTime(); // 本地時間戳(毫秒)
var nyear = <%=Year(d)%>;
var nmonth = <%=Month(d)%>; // 1-12
var nday = <%=Day(d)%>;
var nwday = <%=Weekday(d)%> - 1; // JS 0=周日
var nhrs = <%=Hour(t)%>;
var nmin = <%=Minute(t)%>;
var nsec = <%=Second(t)%>;
// 服務器時間 - 本地時間 = 偏差(毫秒)
var dectime = new Date(nyear, nmonth-1, nday, nhrs, nmin, nsec).getTime() - t0;
var nday = -1; // 日期變更檢測標記
var cweekday = ["日","一","二","三","四","五","六"];
// 農曆計算(簡化版,精確範圍 1900-2100)
var lunarInfo = [0x04bd8,0x04ae0,0x0a570,...]; // 省略完整數組
function lYearDays(y){...} function leapMonth(y){...} function monthDays(y,m){...}
function solarToLunar(y,m,d){...} // 返回 {lMonth, lDay, isLeap}
var lunar = solarToLunar(nyear, nmonth, nday);
var lmonth = ["正","二","三","四","五","六","七","八","九","十","冬","臘"][lunar.lMonth-1];
var lday = ["初一","初二",...,"三十"][lunar.lDay-1];
function shapetime(h,m,s){
return (h<10?"0":"")+h+":"+(m<10?"0":"")+m+":"+(s<10?"0":"")+s;
}
function NewTick(){
var now = new Date();
var t1 = now.getTime();
now.setTime(t1 + dectime); // 應用偏差得到服務器時間
if(now.getDate() != nday){
nyear = now.getFullYear();
nmonth = now.getMonth()+1;
nwday = now.getDay();
nday = now.getDate();
// 重新計算農曆
lunar = solarToLunar(nyear, nmonth, nday);
lmonth = ["正","二","三","四","五","六","七","八","九","十","冬","臘"][lunar.lMonth-1];
lday = ["初一","初二",...,"三十"][lunar.lDay-1];
}
nhrs = now.getHours(); nmin = now.getMinutes(); nsec = now.getSeconds();
}
function Draw(){
NewTick();
var s = nyear + "年" + nmonth + "月" + nday + "日 星期" + cweekday[nwday] + " " + shapetime(nhrs,nmin,nsec);
s += " 農曆" + lmonth + "月" + lday;
document.getElementById("clock").innerHTML = s;
}
document.write("");
Draw();
setInterval(Draw, 500);
</script>
| 問題 | 解決方案 |
|---|---|
| 網絡延迟導致初始時間偏差 | ASP 輸出服務器時間,JS 計算偏差 dectime,後續本地走時 + 偏差 = 服務器時間 |
| 頁面被瀏覽器/代理緩存 | Response.CacheControl="no-cache" + Pragma:no-cache + Expires=-1 |
其他站點盜用 <script src=...> | 檢查 HTTP_REFERER 包含本域名,否則 403 |
| 跨天/月/年日期變更 | NewTick 中檢測 getDate() != nday 重新計算日期與農曆 |
| 農曆算法複雜 | 嵌入精簡版農曆查表算法(約 2KB),無需服務端計算 |
server_time.js.asp,頁面引用 <script src="server_time.js.asp"></script>。/api/servertime 返回 JSON),前端 fetch 獲取,減少頁面體積。' 僅需時間戳,無農曆/格式化
Response.Write "var serverTime=" & DateDiff("s", "1970-1-1", Now()) & ";"
// 前端
var dectime = serverTime*1000 - Date.now();
setInterval(()=>{ document.getElementById('clock').textContent = new Date(Date.now()+dectime).toLocaleString(); }, 1000);
根據業務複雜度選擇方案,核心思想始終是:服務端權威時間源 + 客戶端高頻渲染 + 安全防護。