在30秒后显示Div

时间:2015-06-02 09:06:41

标签: javascript jquery html css

我正在尝试为用户发送欢迎信息。

我的计划是:

  • 如果用户第一次访问该页面,则会在30秒后显示div消息;
  • 如果用户已访问该页面,则无需显示div消息。

我写了这段代码:

我的Cookie代码

<script type="text/javascript">
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    } else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}
</script>

我的显示代码

<script type="text/javascript">
function showbox() {
    document.getElementById("apDiv1").style.visibility = "visible";

}
setTimeout("showbox()", 30000); // after 5 secs
</script>
<script type="text/javascript">
if (!readCookie('visitedPreviously')) {
    showbox();
    //document.write(' Your Message Goes Here And You See It Only Once ');
    createCookie('visitedPreviously', 'visitedPreviously', 3); // 365 days persistence
}

//eraseCookie('visitedPreviously');  // FOR TEST PURPOSES
</script>

我的问题是当访问者第一次访问该页面时,div立即显示(30秒) - 不起作用

但是当回访者再次访问页面时div会在30秒后显示。

我想第一次用户访问,以便在30秒后显示div消息。

请帮帮我。

7 个答案:

答案 0 :(得分:3)

根据您的解释,您希望仅在30秒后向新用户显示一个对话框。因此,您需要将代码更改为:

<script type="text/javascript">
function showbox() {
    document.getElementById("apDiv1").style.visibility = "visible";
}
if (!readCookie('visitedPreviously')) { //if he/she is a new user
     setTimeout("showbox()", 30000); // after 30 secs
    //document.write(' Your Message Goes Here And You See It Only Once ');
    createCookie('visitedPreviously', 'visitedPreviously', 3); // 365 days persistence
}

//eraseCookie('visitedPreviously');  // FOR TEST PURPOSES
</script>

答案 1 :(得分:2)

您需要删除showbox周围的引号。

这是setTimeout fnction的语法,

setTimeout(function,milliseconds,param1,param2,...)

setTimeout要求第一个参数是函数调用。

&#13;
&#13;
function showbox() {
  document.getElementById("apDiv1").style.visibility = "visible";
  alert();

}
setTimeout(showbox, 30000); // after 5 secs

if (!readCookie('visitedPreviously')) {
  //I added a set timeout here which is what you need
  setTimeout(showbox, 30000); // after 5 secs
  //document.write(' Your Message Goes Here And You See It Only Once ');
  createCookie('visitedPreviously', 'visitedPreviously', 3); // 365 days persistence
}

//eraseCookie('visitedPreviously');  // FOR TEST PURPOSES
&#13;
&#13;
&#13;

答案 2 :(得分:1)

删除"showbox()"中的引号和括号。像这样:

setTimeout(showbox, 30000);

答案 3 :(得分:1)

我已经更正了下面的代码,您在检查过Cookie之前调用了setTimeout!

&#13;
&#13;
function showbox() {
    document.getElementById("apDiv1").style.visibility = "visible";
}

if (!readCookie('visitedPreviously')) {
    setTimeout("showbox()", 30000); // after 30 seconds
    //document.write(' Your Message Goes Here And You See It Only Once ');
    createCookie('visitedPreviously', 'visitedPreviously', 3); // 365 days persistence
}
&#13;
&#13;
&#13;

请试一试。

答案 4 :(得分:0)

您不需要showbox周围的引号:

setTimeout(showbox, 30000);

文档:https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout

答案 5 :(得分:0)

您应该将显示代码修改为:

<script type="text/javascript">
function showbox() {
    document.getElementById("apDiv1").style.visibility = "visible";

}
setTimeout(showbox, 30000); // after 5 secs
</script>
<script type="text/javascript">
if (!readCookie('visitedPreviously')) {
    //I added a set timeout here which is what you need
    setTimeout(showbox, 30000); // after 5 secs
    //document.write(' Your Message Goes Here And You See It Only Once ');
    createCookie('visitedPreviously', 'visitedPreviously', 3); // 365 days persistence
}

//eraseCookie('visitedPreviously');  // FOR TEST PURPOSES
</script>

请注意,上面的更改是:

  

//我在这里添加了一个设置超时,这就是你需要的   
setTimeout(showbox,30000); // 5秒后

将函数作为字符串发送是一种不错的做法,就像在设置超时中一样。

答案 6 :(得分:0)

您需要将代码修改为

int main()
{
    derived_op d;
    base_op<base_info<derived_info> > b;
}