2016-12-29 75 views
1

我可以在下面添加什麼來防止在除夕晚上8點之後彈出窗口?爲具有Cookie的彈出窗口設置到期日期(和時間)

$(document).ready(function(){ 
    // Start Cookie for PopUp 
    if (document.cookie.indexOf('colorframe=true') === -1) { 
    var expires = new Date(); 
    expires.setDate(expires.getDate()+1); 
    document.cookie = "colorframe=true; escKey=true; expires="+expires.toUTCString(); 

     // Start Popup 
     setTimeout(function() { 
      $.colorbox({ 
       escKey: true, 
       html: '<a href="---"><img src="---" width="550" height="550" alt="New Years Eve at ---"/></a>' 
      }); 
     }, 2000); 
    }; 
}); 
+0

根據當地時區或服務器的時區爲晚上8點? – yts

+0

偉大的問題。格林威治標準時間5或EST ... – mdnash

回答

2

您可以比較當前時間戳和晚上8點的時間戳。使用http://www.epochconverter.com/我相信這是1483232400000.

$(document).ready(function(){ 
    // Start Cookie for PopUp 
    if ((new Date).getTime() > 1483232400000) return; 

    //remainder of code here 

}); 

這是假設你信任你的客戶的計算機上的時間。

+0

什麼是更好,如果今晚在8我添加一個代碼,說'48小時後不顯示以下彈出'---我會怎麼做? @yts – mdnash

+0

@mdnash我不認爲這會更好。我認爲這是理想的,如果你可以在渲染html時設置服務器時間檢查(設置某種全局JavaScript變量),以便不依賴於客戶的時間 – yts

+0

我可以從服務器時間辨別服務器的時間whois搜索?如果是的話,那麼GMT ... – mdnash