Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 427
Attila Naghi Avatar asked Mar 13 '14 07:03

Attila Naghi


People also ask

How do I set cookie expiry time?

Just set the expires parameter to a past date: document. cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"; You should define the cookie path to ensure that you delete the right cookie.

Is cookie Max Age in seconds?

Sets the maximum age of the cookie in seconds. This will result in a cookie1 to expire in 20 seconds.

How do cookies expire at end of session?

To set a cookie so it expires at the end of the browsing session, simply OMIT the expiration parameter altogether.

What is the default expiry time of cookie?

What is the timeout of Cookie? The default time for a Cookie to expire is 30 minutes. The default Expires value for a cookie is not a static time, but it creates a Session cookie. This will stay active until the user closes their browser/clears their cookies.


3 Answers

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
like image 199
Felix Avatar answered Sep 21 '22 08:09

Felix


var date = new Date();
date.setTime(date.getTime() + (30 * 1000)); //add 30s to current date-time 1s = 1000ms
$.cookie('username', username, { expires: date });  //set it expiry
like image 35
Tushar Gupta - curioustushar Avatar answered Sep 20 '22 08:09

Tushar Gupta - curioustushar


You can Use as below for 1 minute and 30 seconds:

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

//3.5* 60 * 1000 = 1 minute and 30 seconds

//For 30 Seconds

  var date = new Date();
 var minutes = 0.5;
 date.setTime(date.getTime() + (minutes * 60 * 1000));
 $.cookie('username', username, { expires: date });
like image 41
Butani Vijay Avatar answered Sep 20 '22 08:09

Butani Vijay