How to Expire a Cookie in 30 Minutes Using Jquery

How to expire a cookie in 30 minutes using jQuery?

30 minutes is 30 * 60 * 1000 miliseconds. Add that to the current date to specify an expiration date 30 minutes in the future.

 var date = new Date();
var minutes = 30;
date.setTime(date.getTime() + (minutes * 60 * 1000));
$.cookie("example", "foo", { expires: date });

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

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

jscookie.js how to expire cookie in minutes not days?

Found the answer in:
Frequently-Asked-Questions

JavaScript Cookie supports a Date instance to be passed in the expires
attribute. That provides a lot of flexibility since a Date instance
can specify any moment in time.

Take for example, when you want the cookie to expire 15 minutes from
now:

var inFifteenMinutes = new Date(new Date().getTime() + 15 * 60 * 1000);
Cookies.set('foo', 'bar', {
expires: inFifteenMinutes
});

Also, you can specify fractions to expire in half a day (12 hours):

Cookies.set('foo', 'bar', {
expires: 0.5
});

Or in 30 minutes:

Cookies.set('foo', 'bar', {
expires: 1/48
});

Trying to expire of a cookie within 3 minutes

Take a look at this answer here:
How do I set a cookie to expire after 1 minute or 30 seconds in Jquery?

Here is an example of a cookie set to 3 minutes using the jQuery cookie plugin:

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

The plugin:
https://github.com/carhartl/jquery-cookie

Check the usage of the plugin. That should get you started.

jQuery cookie expiry time

The plugins expires option accepts either a number or a date object.

If a number is passed, it's the number of days until the cookie expires, but if a date object is passed, it's the time and date when the cookie expires, so you can do

var expDate = new Date();

expDate.setTime(expDate.getTime() + (15 * 60 * 1000)); // add 15 minutes

$.cookie(COOKIE_NAME, 'test', { path: '/', expires: expDate });

Jquery - cookies set expired date for 1 day

24 hours is 24 * 60 * 60 * 1000 milliseconds. Look at this

If you need add few days to date of expires cookie you should use number

$.cookie('choosen-Accept', 'yes', {
expires: 1
});

If you need custom time you should create date and pass it to expired

var date = new Date();
date.setTime(date.getTime() + 24 * 60 * 60 * 1000);
$.cookie('choosen-Accept', 'yes', {
expires: date
});

To make sure you can look at this source code line 6

Full example

  $('#accept').click(function() {
if (!$('.change-message--on-click').is('hide--first')) {
$('.change-message--on-click').removeClass('hide--second');
$('.change-message--on-click').addClass('hide--first');

var date = new Date();
date.setTime(date.getTime() + 24 * 60 * 60 * 1000);

$.cookie('choosen-Accept', 'yes', {
expires: date
});
}
return false
});


Related Topics



Leave a reply



Submit