Storage.prototype.setExpire = function (arrObj) {
    var date,
        fa, ta,
        newItems = [],
        storage = localStorage.getItem('limitStorage');
    items = JSON.parse(storage || "[]");
    date = new Date();
    date = date.toString().split(' ');
    date = Number(date[2]);
    for (var key in arrObj) {
        for (i = 0; i < items.length; i++) {
            if (items[i].value == key) {
                if (typeof (items[i].days) == "number") {
                    if (items[i].date !== date) {
                        days = (date - items[i].date);
                    } else {
                        days = items[i].days;
        ta = {
            value: key,
            limit: arrObj[key],
            date: date,
            days: days !== undefined ? days : 0
        if (ta.days >= ta.limit) {
        if(localStorage.getItem(ta.value) !== null) newItems.push(ta);
    localStorage.setItem('limitStorage', JSON.stringify(newItems));

This code runs by simply calling the Storage method and then adding method .setExpire.



I have to add more to the code to take into account day 31 to day 1. But as of now, if the item is added on day 20 and you want it to expire in 7 days on day 27, the localStorage item will be removed.

My question is: does anyone see anything they can review for me? Maybe a cross-browser compatibility issue? I already have a cross-browser localStorage that falls back to cookies if need be.

Also let me know if you may think of a way for the code to take into account the ending to the start of a months, since the code technically only takes the new date and then subtracts the stored date, which gets our days that have passed.

Any and all suggestions are welcomed.

  • \$\begingroup\$ changed my code more. Now the code if you change "item1":3 to less or more days "item1":6 code will update the item. \$\endgroup\$ – EasyBB Dec 28 '13 at 1:36

I think this code could use a lot of polishing.

Getting the localStorage

 storage =localStorage.getItem('limitStorage');
 getItems = storage !== null && storage !== "[]" ? localStorage.getItem('limitStorage') : null;
 getItems = getItems !== null ? JSON.parse(getItems) : null;

can easily be replaced with:

items = JSON.parse( localStorage.getItem('limitStorage') || "[]" );

I renamed getItems to items ( getItems is a name better suited for a function ), the code also defaults items to an empty array if nothing was found.

Setting an expiry date

The easiest way to deal with that is to get now in milliseconds, add 1000 * 60 * 60 * 24 * days to it, and set that as the expiry date. Then you only have to compare (new Date()).getTime() with the expiry date.

So you have something like

var now = (new Date()).getTime();
var day = 1000 * 60 * 60 * 24;

for (var key in arrObj) {
  newItems.push( { value : key , expires : now + day * arrObj[key] } );

Now, we are facing 2 problems:

  • If you want to change the expiry date of an item, you cant..
  • Much worse, once you set some items to expire, you can never add extra items to expire since you will always fall into the else branch.

At this point, I think the whole code needs a re-write and re-think.

  • \$\begingroup\$ well to add more items to expire, we could write another if condition or a more suitable if condition to push it to the array of objects. Like you said it does need to be polished, I only wrote it very quickly as I was tired of not having that option to set items to expire. Though I don't think it's a bad start. I will work on this some more to be able to change the expire day, and add more options to the expire, persay minutes hours days. Maybe, though I will take account what you've told me. Very helpful tomdemuyt! Thank you. \$\endgroup\$ – EasyBB Dec 27 '13 at 20:00
  • \$\begingroup\$ The concept is good, please do update your question once you have this more fleshed out. \$\endgroup\$ – konijn Dec 27 '13 at 22:43
  • \$\begingroup\$ ok so I've changed the code drastically, took in account some of what you said, still more needs to be edited. Though now if the setExpire days is changed it'll update it. Also it won't save any items that do not actually exist until they exist. \$\endgroup\$ – EasyBB Dec 28 '13 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.