讓瀏覽器的時間基本不受系統時間影響

作網頁時鐘,要想時間準確,就從服務器上拉時間數據。php

客戶機修改本地時間後,有的時鐘就不許了,通常狀況下不要一直從服務器上拉時間數據,服務器會累。css

那麼怎麼辦的?html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>TrueTime</title>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
var serTime = new Date(<?php echo time();?> * 1000);
var locTime = new Date();
var deltaTime = locTime - serTime;
var gon = 0;
var gos = 0;
var gonp;
var gol = 400;
function go(){
	gon += 1;
	let now = new Date();
	if (Math.abs(now.getTime() - locTime) >= gol) {
		if(Math.floor(Math.abs(now.getTime() - locTime) / gol) == 1){
			gos = Math.floor((now.getTime() - (serTime.getTime() + deltaTime)) / gol);
			gonp = gon / gos;
			locTime = new Date(locTime.getTime() + gol);
		}else{
			let truetime = Math.floor(gon / gonp * gol + gol/2 ) + serTime.getTime();
			locTime = new Date();
			deltaTime = locTime.getTime() - truetime;
		}
	}
	window.requestAnimationFrame(go);
}
go();

function getTrueTime(){
	return new Date(new Date() - deltaTime);
}
</script>
</head>
<body>
</body>
</html>
相關文章
相關標籤/搜索