2016-03-08 45 views
0

在我的頁面上,我添加了帶時間延遲內容的DIV。 直到定時器用完爲止,內容顯然不會顯示。 查看下面的代碼。div在重訪時立即顯示

現在的問題是,我想立即在 重新訪問頁面(可能通過Cookie?)顯示div。

不幸的是這個編碼是對我來說太複雜,我希望有人可以幫助我了...(的jsfiddle者優先)提前

感謝,如果你能幫助我!

function showIt() { 
 
    document.getElementById("optin").style.visibility = "visible"; 
 
} 
 
setTimeout("showIt()", 2000); // after 110 sec (1m50s)
HEY 
 
<div id="optin" style="visibility: hidden;"> 
 
    <p>You can only see this after the timer runs out!</p> 
 
    </div>

+0

不是真的複雜...創建的cookie ...讀取Cookie ...執行條件的東西...更好 – Rayon

+1

用的sessionStorage或大於一個cookie的localStorage ,爲此目的代碼少,性能好。 –

+0

@MarcosPérezGude,對!@ @ OP,請參考:https://jsfiddle.net/vu26prtn/ – Rayon

回答

0

您的計時器

setTimeout("showIt()", 2000); // after 110 sec (1m50s) 

2000是2000毫秒,2秒。你需要110秒110秒。

至於設置會話變量:

//checks to make sure the browser supports session storage 
if(typeof(Storage) !== "undefined") { 
    //checks to see if the current session variable exists 
    if (localStorage.pageVisited) { 
     //display the element 
     document.getElementById('optin').style.visibility = "visible"; 
    } else { 
     //creates the pageVisited session variable 
     localStorage.pageVisited = 1; 
    } 
}