Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/expire #5

Closed
wants to merge 7 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 20 additions & 6 deletions client-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,21 +216,20 @@ ClientStorage.prototype.get = function (key) {
ClientStorage.prototype.set = function (key, value, time) {
if (time) {
if (this.LSSupport) {
let expire = new Date().getTime() + time;
this.ls.setItem(key, this.__escape(value));
setTimeout(() => {
this.ls.removeItem(key)
}, time)
this.ls.setItem(key + "._expiredAt", expire);
} else if (this.cookies) {
this.cookies.set(key, this.__escape(value));
setTimeout(() => {
this.cookies.remove(key, null, window.location.host)
}, time)
let seconds = time / 1000;
document.cookie = key + "=" + value + "; max-age=" + seconds + "; path=/";
hakod marked this conversation as resolved.
Show resolved Hide resolved
} else {
this._data[key] = value;
setTimeout(() => {
delete this._data[key];
}, time)
}
return true
hakod marked this conversation as resolved.
Show resolved Hide resolved
}
if (this.LSSupport) {
this.ls.setItem(key, this.__escape(value));
Expand All @@ -242,6 +241,21 @@ ClientStorage.prototype.set = function (key, value, time) {
return true;
};

function LSExpire() {
setInterval(() => {
for (var key in localStorage) {
if (key.slice(-11) === "._expiredAt") {
hakod marked this conversation as resolved.
Show resolved Hide resolved
if (window.localStorage.getItem(key) < new Date().getTime()) {
window.localStorage.removeItem(key);
window.localStorage.removeItem(key.replace("._expiredAt", ""))
}
}
}
}, 2000)
}

LSExpire();
hakod marked this conversation as resolved.
Show resolved Hide resolved


/*
@function
Expand Down