0

I just tried to make a timer with cookies that makes a button only 3 times clickable (I had to do it with cookies cause it refreshes the page in its process), I made this timer but it doesn't work. Nothing on my page changed at all.

The code I have by //something else happens gets executed by the program.

Timer - (or at least what I thought that would work as a timer) :

mailagain.onclick = function () {
    if (typeof getCookie("countIt") !== 'undefined') {
        if (checkCookie("countIt") > 3) {
            // something happens
        } else {
            //something else happens
            var counter = checkCookie("countIt") + 1;
            setCookie("countIt", counter, 1)
        }
    } else {
        setCookie("countIt", 1, 1)
    }
};

Coockie functions :

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

function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.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 "";
}

function checkCookie(name) {
    var value = getCookie("name");
    if (value != "") {
        return value;
    }
}
Minegolfer
  • 276
  • 4
  • 18
  • And what exactly does not work? Have you checked the browser console for any errors? – Reporter May 18 '17 at 11:34
  • I don't know what exactly doesn't work, I think the cookie never gets set or something. This is my first time working with cookies so the problem is probably simple. Also the browser doesn't give any errors – Minegolfer May 18 '17 at 11:35
  • I did the `getCookie("countIt")` in the debugger of chrome and it said `getCookie("countIt"): "NaN"` even after pressing the button that should make the timer go up – Minegolfer May 18 '17 at 11:39

2 Answers2

3

Some issues:

  • When reading the value from the cookie, be aware that it has the string data type. You need to convert it to number before comparing it with another number or adding 1 to it.
  • The function checkCookie is using the wrong (hard-coded) cookie name, but is even not necessary as a function. You can do all that with getCookie.

Here is a working version:

mailagain.onclick = function () {
    // make sure to convert to number (unitary plus), or use 0 when it is not a number:
    var counter = (+getCookie("countIt") || 0) + 1;
    setCookie("countIt", counter, 1)
    if (counter > 3) {
        console.log('clicked too many times! (', counter, ')');
    } else {
        console.log('clicked ' + counter + ' number of times.');
    }
};
trincot
  • 317,000
  • 35
  • 244
  • 286
  • Thanks I made it so it does what my code should do and it worked perfectly :) Also can I somehow change reset cookies to 0 in my console or maybe change the time its valid to 1 minut? cause then its easier to test my stuff. – Minegolfer May 18 '17 at 11:57
  • You're welcome. You can add code to [delete the cookie](http://stackoverflow.com/questions/2144386/javascript-delete-cookie) via another button, so you can test from scratch. To make the cookie expire sooner, pass a smaller number as last argument to `setCookie`: `1/(24*60)` would be 1 minute. – trincot May 18 '17 at 12:57
0
var value = getCookie("name"); 

getCookie always return "undefined" because of wrong cookie name. Remove brakets.

function checkCookie(name) {
    var value = getCookie(name); //here you go
    if (value != "") {
        return value;
    }
}
vovchisko
  • 2,049
  • 1
  • 22
  • 26