javascriptjquerypopupwindow

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

Solution

  • 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 "";
    }