關於倒計時獲取head時間

關於倒計時獲取head時間

在作網站倒計時的時候咱們通常須要得到一個服務端的時間在作倒計時.這時你可能會想到用ajax請求服務端的接口,返回服務端的時間不就行了嗎?javascript

可是這樣真的好嗎?你是否記得http這個寶物呢,沒錯咱們只須要得到http中的時間部分就能夠.java

code View

var xhr = new XMLHttpRequest();
if( !xhr ){
    xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.open("HEAD",location.href,true);
xhr.onreadystatechange=function(){
    if( xhr.readyState == 4 && xhr.status == 200 ){
        var time = (xhr.getResponseHeader('Date'));
                
    }
};
xhr.send(null);

是否是很酷呢~ajax

相關文章
相關標籤/搜索