問題描述
我的任務(wù)是在某個(gè)固定時(shí)區(qū)(MSK 或 MSD - 取決于當(dāng)前日期)的 HTML 頁面上顯示數(shù)字時(shí)鐘(精確到分鐘).我想避免依賴客戶端系統(tǒng)時(shí)鐘,因此需要與服務(wù)器進(jìn)行一些同步.HTTP 服務(wù)器在每個(gè)響應(yīng)中發(fā)送 Date 標(biāo)頭,因此我們可以向我們網(wǎng)站的任何 URL 發(fā)送 AJAX GET 或 HEAD 請(qǐng)求以獲取服務(wù)器日期,計(jì)算與客戶端日期的差異,并在使用 setTimeout() 更新時(shí)鐘時(shí)使用它.還有其他問題:日光設(shè)置的時(shí)區(qū)切換、連接速度非常慢的延遲.
I have a task to show digital clock (with minutes precision) on HTML page in some fixed timezone (MSK or MSD - depending on current date). I'd like to avoid relying on client system clock, so some synchronization with server is required. HTTP server sends Date header in each response so we can send an AJAX GET or HEAD request to any URL of our site to get server date, calculate the difference with client date and use it when updating clock with setTimeout(). There are other issues remains: timezone switching for daylight settings, latency accounting for very slow connections.
對(duì)這項(xiàng)任務(wù)有最簡(jiǎn)單的方法嗎?我寧愿在沒有服務(wù)器端編程的情況下解決它.
Any idea to this task the simpliest way? I'd prefer to solve it without server-side programming.
推薦答案
如果你打算使用 ajax,你應(yīng)該記住客戶端時(shí)間在 readyState==2 和 readyState==3 之間,因?yàn)榉?wù)器時(shí)間會(huì)設(shè)置在時(shí)間之間收到請(qǐng)求并準(zhǔn)備好響應(yīng)
you should remember client time between readyState==2 and readyState==3 if you are going to use ajax, because server time will be set somewhere between time on request recieved and response prepared
這篇關(guān)于將客戶端 javascript 時(shí)鐘與服務(wù)器日期同步的最佳方法的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!