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
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.
Sets the maximum age of the cookie in seconds. This will result in a cookie1 to expire in 20 seconds.
To set a cookie so it expires at the end of the browsing session, simply OMIT the expiration parameter altogether.
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.
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
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
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 });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With