/    Sign up×
Community /Pin to ProfileBookmark

timed div overlay loop using cookies

Hi,

I’m trying to create a div popup (yes, I know) that repeatedly appears every 7 minutes. It needs to work regardless of whether a user refreshes or navigates to different pages within the site, so I’m attemping to use a cookie to stop the timer from restarting every time the user changes page or refreshes.

My thinking’s like this:

  • 1. set cookie = current time

  • 2. every 5 seconds check to see if current time is >= to cookie + 7 minutes

  • 3. When it is popup div and clear cookie

  • 4. On close of div popup repeat previous steps
  • With my VERY limited knowladge of javascript this is what I’ve got as the JavaScript for the cookie/popup trigger: It doesn’t even appear to be setting the cookie currently. Can anyone help and tell me what am I doing wrong? I’d really appreciate it. Thanks.

    [CODE]

    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);
    }

    function popup_cookie() {
    var x = getTime;
    createCookie(‘popup_cookie_01’,x,0);
    }

    function open_float() {
    setVisible(‘floating_layer’);
    return false;
    }

    function popup() {
    var y = getTime;
    var x = readCookie(‘popup_cookie_01’);
    if y >= (x + 420000) {
    open_float();
    eraseCookie(‘popup_cookie_01’);
    }
    }

    function check_popup() {
    setInterval(“popup()”,5000)
    }

    function exit_float() {
    popup_cookie();
    setVisible(‘floating_layer’);
    return false;
    }

    addLoadEvent(popup_cookie)
    addLoadEvent(check_popup)

    [/CODE]

    to post a comment
    JavaScript

    4 Comments(s)

    Copy linkTweet thisAlerts:
    @cgishackJan 06.2008 — Here is an example of how to set a cookie.

    Call this function off any element you want.

    <i>
    </i>function SetCookie()
    {
    //alert("Setting Cookie");
    var date = new Date();
    date.setTime(date.getTime()+(1*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    var value = "someValue";
    var name = "TimedMessage";
    document.cookie = name+"="+value+expires+"; path=/";
    }


    Your problem might be the addLoadEvent() function.

    Maybe something is going wrong there..... call the popup_cookie off something else to see if its getting set.

    Drew
    Copy linkTweet thisAlerts:
    @unxposedauthorJan 06.2008 — Thanks, I almost have it now (see below) - just one last issue (I think) - The problem is with this code:

    [CODE]
    var d = new Date();
    var y = d.getTime();
    var x = (y + 1);
    [/CODE]


    Why, as an example, when y = 1000 does x = 10001 - and how can I fix this? I need it to add the numbers togetherm not add the one on the end of the number.

    Thanks again!

    Full code:

    [CODE]

    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);
    }

    function popup_cookie() {
    var d = new Date();
    var x = d.getTime();
    createCookie('popup_cookie_01',x,0);
    }

    function open_float() {
    setVisible('floating_layer');
    return false;
    }

    function popup() {
    var d = new Date();
    var y = d.getTime();
    var x = readCookie('popup_cookie_01');
    var z = (x + 1);
    alert(z);
    if (y >= z) {
    open_float();
    clearInterval(timer_01);
    eraseCookie('popup_cookie_01');
    }
    }

    function check_popup() {
    timer_01 = setInterval("popup()",1000);
    }

    function exit_float() {
    popup_cookie();
    check_popup();
    setVisible('floating_layer');
    return false;
    }


    [/CODE]
    Copy linkTweet thisAlerts:
    @scragarJan 06.2008 — var x = (parseInt(y, 10) + 1);
    Copy linkTweet thisAlerts:
    @unxposedauthorJan 06.2008 — Thanks thanks thanks!
    ×

    Success!

    Help @unxposed spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.27,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...