jquerycookiesjquery-cookie

How do I set a cookie to expire after 1 minute or 30 seconds in Jquery?


How do i set my cookie to expire after 30 sec or 1 m ? this is my code :

$.cookie('username', username, { expires: 14 });  // expires after 14 days

Solution

  • For 1 minute, you can use:

    var date = new Date();
    date.setTime(date.getTime() + (60 * 1000));
    $.cookie('username', username, { expires: date });  // expires after 1 minute
    

    For 30 seconds, you can use:

    var date = new Date();
    date.setTime(date.getTime() + (30 * 1000));
    $.cookie('username', username, { expires: date });  // expires after 30 second