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

Attila Naghi picture Attila Naghi · Mar 13, 2014 · Viewed 52.7k times · Source

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

Answer

Felix picture Felix · Mar 13, 2014

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