-2

I have to create a JS cookie when a user click a button, this cookie will remember him after 10 minutes with another popup.

Example:

<button>Click me!</button>

and the button will hide when user click, after 10 minutes the button will show again:

<button>Click me!</button>

Script part:

function setCookieMsg(name) {
        var d = new Date();
        var time = d.setTime(d.getTime() + (600000));
        document.cookie = name + "=" + time;
    }

    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) != -1) return c.substring(name.length, c.length);
    }
    return "";
    }

    function checkCookieMsg() {
    var cookie = getCookie("name");
    var d = new Date() - 600000;
    if (cookie == "") {
        setCookie("name", cookie);
    }else if (d => cookie) {
        $().getUnReadMessage();
        }
    }

What I've wrong?

Filburt
  • 17,626
  • 12
  • 64
  • 115
Italiano
  • 55
  • 3
  • 11
  • I don't know what you've wrong. What have you wrong? Does it not work? What happens instead? Do you see errors in the browser's error console? – JJJ Oct 03 '14 at 08:53
  • what about localStorage? – Gildas.Tambo Oct 03 '14 at 08:53
  • try https://github.com/carhartl/jquery-cookie – user733421 Oct 03 '14 at 08:57
  • Since you would continuously have to check your cookie why not skip the cookie altogether and just use `setTimeout()` and have the callback handle showing the button again? [Calling functions with setTimeout](http://stackoverflow.com/q/3800512/205233) has some examples. – Filburt Oct 03 '14 at 08:58

1 Answers1

0

You didn't mention the expires in cookie. The expires date should be UTC time string.

function setCookieMsg(name) {
     var date = new Date();
     date.setTime(date.getTime()+(600000));
     var expires = "; expires="+date.toUTCString();
     document.cookie = name + "=" + expires;
}
SSA
  • 5,433
  • 4
  • 36
  • 50