ania_piszko
ania_piszko

Reputation: 31

How to disable popup from showing up on given page for 2 days after I click .popup-close on first visit to that page?

How to disable popup from showing up on given page for 2 days after I click .popup-close on first visit to that page ?

This is my code https://jsfiddle.net/4q1aL8pL/2/

I've tried localstorage between lines 122-140 in my code. I am javascript beginner please help :)

Maby there has to be some timer applied which will count to 2 days hours worth ?

    //<![CDATA[
        var n = Number(localStorage.getItem('odometerValue')) || 0;
   var m = localStorage.getItem('displayNone');
        var myOdometer;
        function startcounting () {
            var div = document.getElementById("odometerDiv");
            myOdometer = new Odometer(div, {value: n, digits: 6, tenths: true});
    myOdometer.set(n);        
            update();
        }

        function update () {
            n=n+0.01
            myOdometer.set(n);
    localStorage.setItem('odometerValue', n);
    localStorage.setItem('displayNone', m);        
            setTimeout(update, 200);
        }
    //]]>

Upvotes: 0

Views: 478

Answers (2)

Damajj
Damajj

Reputation: 68

Here is a function for using cookies.

/* Show popup if cookie doesn't exist. Will hide for 2 days if closed */
var PopUpCookie = getCookie("MyPopUpCookie");
if (PopUpCookie == '') {
        $('#odometerDiv').show();
    } else {
        $('#odometerDiv').hide();
    }
}

$('.popup-close').on('click', function () {
    $('#odometerDiv').hide();
    setCookie("MyPopUpCookie", "hide");
});

function setCookie(cname, cvalue) {
    var d = new Date();
    d.setTime(d.getTime() + (2*24*60*60*1000)); /* 2 days */
    var expires = "expires=" + d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

function getCookie(cname) {
    var name = cname + "=";
    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);
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return "";
}

Upvotes: 1

schylake
schylake

Reputation: 444

you can use the local storage to save the date of when the pop up loaded.

var d = new Date();

this will stamp the current date then all you have to do is check with the date of the visitor again and if you minus them and it equals 2 days pop up again.

Upvotes: 2

Related Questions