Henry Blaike
Henry Blaike

Reputation: 117

Make a cookie expire in one hour

I have some code for a cookie that i want to make expire in 1 hour the code is:

    function createCookie(name,value,days) {
    if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*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;
    }

    sWhere = "http://google.com";

    if(!readCookie('viewcheck')){
    createCookie('viewcheck',1,1);
    } else {
    window.location = sWhere;
    }

How can i make the cookie expire in an hour?

Upvotes: 0

Views: 1429

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324840

Your function is using a days argument that is only changing minutes, not days since 60*1000 is 60 seconds in milliseconds. You should rename that argument to minutes, and then you can easily see that you just need to multiply the numberof hours by 60.

Upvotes: 1

Related Questions