@jbtje/vue3tabs
Version:
Simple vue3 component to display tabs
45 lines (34 loc) • 1.05 kB
JavaScript
// Remove expired items.
Object.keys( localStorage ).forEach( function( str ) {
let item = localStorage.getItem( str );
let date = new Date();
if( item?.expires < date ) {
localStorage.removeItem( str );
}
} );
class ExpiringStorage {
get( key ) {
const cached = JSON.parse(
localStorage.getItem( key ),
);
if( !cached ) {
return null;
}
const expires = new Date( cached.expires );
if( expires < new Date() ) {
localStorage.removeItem( key );
return null;
}
return cached.value;
}
set( key, value, lifeTimeInMinutes ) {
const currentTime = new Date().getTime();
const expires = new Date( currentTime + lifeTimeInMinutes * 60000 );
localStorage.setItem( key, JSON.stringify( {value, expires} ) );
}
remove( key ) {
localStorage.removeItem( key );
return true;
}
}
export default new ExpiringStorage();