具备有效期的sessionStorage存储

类方式

// 具备有效期的sessionStorage存储-类方式。
class SessionStorageWrapper {
// 存储数据到sessionStorage,记录下当前存储的时间。
static setItem(key, value) {
try {
const item = {
value,
time: Date.now(),//过期时间-如果不输入则为无限。
};
sessionStorage.setItem(key, JSON.stringify(item));
} catch (error) {
console.error('存储数据到sessionStorage时出错:', error);
}
} // 从sessionStorage中获取数据,如果数据过期则返回null
static getItem(key, cycle) {
try {
cycle = Number(cycle)
if (isNaN(cycle)) {
cycle = 1000 * 60 * 60 * 24 * 30//默认过期时间为一个月。
}
let data = sessionStorage.getItem(key)
if (!data) {
return null
}
let { time, value } = JSON.parse(data)
if ((Date.now() - time) > cycle) {
sessionStorage.removeItem(key)
return null
}
return value
} catch (error) {
console.error('从sessionStorage中获取数据时出错:', error);
return null;
}
} // 从sessionStorage中移除数据
static removeItem(key) {
try {
sessionStorage.removeItem(key);
} catch (error) {
console.error('从sessionStorage中移除数据时出错:', error);
}
} // 清空sessionStorage中的所有数据
static clear() {
try {
sessionStorage.clear();
} catch (error) {
console.error('清空sessionStorage时出错:', error);
}
}
}
// 示例用法:
const userData = {
name: '张三',
age: 30,
email: 'zhangsan@example.com'
};
SessionStorageWrapper.setItem('user', userData);
const storedData = SessionStorageWrapper.getItem('user');
console.log(storedData); // 如果数据未过期,则输出userData对象;否则输出null const storedData1 = SessionStorageWrapper.getItem('user',1); // 数据过期,则移除旧数据;
console.log(storedData1);

对象方式

// 具备有效期的sessionStorage存储-对象方式。
export const SessionStorageWrapper= {
// 存储数据到sessionStorage,记录下当前存储的时间。
setItem(key, value) {
try {
const item = {
value,
time: Date.now(),//过期时间-如果不输入则为无限。
};
sessionStorage.setItem(key, JSON.stringify(item));
} catch (error) {
console.error('存储数据到sessionStorage时出错:', error);
}
}, // 从sessionStorage中获取数据,如果数据过期则返回null
getItem(key, cycle) {
try {
cycle = Number(cycle)
if (isNaN(cycle)) {
cycle = 1000 * 60 * 60 * 24 * 30//默认过期时间为一个月。
}
let data = sessionStorage.getItem(key)
if (!data) {
return null
}
let { time, value } = JSON.parse(data)
if ((Date.now() - time) > cycle) {
sessionStorage.removeItem(key)
return null
}
return value
} catch (error) {
console.error('从sessionStorage中获取数据时出错:', error);
return null;
}
}, // 从sessionStorage中移除数据
removeItem(key) {
try {
sessionStorage.removeItem(key);
} catch (error) {
console.error('从sessionStorage中移除数据时出错:', error);
}
}, // 清空sessionStorage中的所有数据
clear() {
try {
sessionStorage.clear();
} catch (error) {
console.error('清空sessionStorage时出错:', error);
}
},
}
// 示例用法:
const userData = {
name: '张三',
age: 30,
email: 'zhangsan@example.com'
};
SessionStorageWrapper.setItem('user', userData);
const storedData = SessionStorageWrapper.getItem('user');
console.log(storedData); // 如果数据未过期,则输出userData对象;否则输出null const storedData1 = SessionStorageWrapper.getItem('user',1); // 数据过期,则移除旧数据;
console.log(storedData1);

具备有效期的sessionStorage存储的更多相关文章

  1. 【转】本地存储-localStroage/sessionStorage存储

    原文地址:[js学习笔记-103]----本地存储-localStroage/sessionStorage存储 客户端存储 l  WEB存储 web存储最初作为html5的一部分被定义成API形式,但 ...

  2. localStorage存储数组,对象,localStorage,sessionStorage存储数组对象

    localStorage存储数组,对象,localStorage,sessionStorage存储数组对象   前言 最近在用angular做商城购物车的功能模块,因为angular的watch监听, ...

  3. localStorage存储对象,sessionStorage存储数组对象

    前言 最近在用angular做商城购物车的功能模块,因为angular的watch监听,数据只要发生变化就能很方便的自动渲染页面.但随即出现的问题是,之前用户操作的样式都会被重置掉. 例如我勾选了几个 ...

  4. sessionStorage存储json对象

    应用场景: 账单列表中A页面:点击其中的一列,ajax返回的数据在这一页 点击进入账单详情B页面: 因为在A页面已经做过ajax的请求了,所以希望把当前其中的一个数组对象传到B页面中,所以,就考虑到暂 ...

  5. 在 sessionStorage存储json对象

    目的:A页面存的东西要从B页面拿到 因为sessionStorage.setItem("key","value")内存储的都是字符串,所以,如果以对象的形式存到 ...

  6. 深入了解浏览器存储:对比Cookie、LocalStorage、sessionStorage与IndexedDB

    摘要: 对比Cookie.LocalStorage.sessionStorage与IndexedDB 作者:浪里行舟 Fundebug经授权转载,版权归原作者所有. 前言 随着移动网络的发展与演化,我 ...

  7. localStorage、sessionStorage、cookie的有效期和作用域问题

    sessionStorage,localStorage,cookie都可以实现客户端存储,三者的区别有哪些了? cookie作为最早期的被设计web浏览器存储少量数据,从底层看,它是作为http协议的 ...

  8. 浅谈浏览器存储(cookie、localStorage、sessionStorage)

    今天我们从前端的角度了解一下浏览器存储,我们常见且常用的存储方式主要由两种:cookie.webStorage(localStorage和sessionStorage).下面我们来一一认识它们. Co ...

  9. 移动端浏览器隐私模式/无痕模式使用本地存储localStorage/sessionStorage的问题

    移动端浏览器隐私模式/无痕模式使用本地存储localStorage/sessionStorage的问题 开发H5 webapp时经常需要使用本地存储,如localStorage和sessionStor ...

  10. H5笔记——locaStorage和sessionStorage本地存储的一些坑

    当使用window.localStorage或者window.sessionStorage 存储json数据时需要将json数据用JSON.stringify(data)转换成json字符串再存储在本 ...

随机推荐

  1. [转帖]--build=arm-linux

    今天在arm上用configure生成makefile时报错:configure: error: cannot guess build type; you must specify one 问题: 不 ...

  2. [官方]Beyond Compare里面 二进制比较的含义.

    Content Comparisons Actions > Compare Contents In the Actions menu, the Compare Contents command ...

  3. 微信小程序之某个节点距离顶部和底部的距离 createSelectorQuery

    这个方法可以用来在上滑滚动的时候,让某一个区域置顶, 在下滑的时候,又变为原来的位置哈! <huadong :class="{'hident':isFixed}" id=&q ...

  4. ClickHouse(06)ClickHouse建表语句DDL详细解析

    目录 当前服务器上创建表(单节点) 语法形式 使用显式架构 从相同结构的表复制创建 从表函数创建 从选择查询创建 分布式集群创建表 临时表 分区表 创建表语句关键字解析 空值或非空修饰符 默认值表达式 ...

  5. # github突破7k star 即时通讯(IM)开源项目OpenIM每周迭代版本发布

    v2.0已经重构完毕,架构更清晰,代码更规范,邀请各位参与OpenIM社区建设有兴趣的同学可以加我私聊. 目前侧正在业务开发,已提供更多功能,包括群管理,阅后即焚,朋友圈,标签下发等. web端体验: ...

  6. 微信小程序-页面跳转navigator组件

    官方文档地址:https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/route.html 在官方文档当中有提到一 ...

  7. 《熬夜整理》保姆级系列教程-玩转Wireshark抓包神器教程(1)-初识Wireshark

    1.简介 前边已经介绍过两款抓包工具,应该是够用了,也能够处理在日常工作中遇到的问题了,但是还是有人留言让宏哥要讲解讲解Wireshark这一款抓包工具,说实话宏哥之前也没有用过这款工具,只能边研究边 ...

  8. Windows安装MySQL到最后卡主无响应处理办法

    安装mysql-5.5.62-winx64到最后Ready to execute ... 生效配置时卡主无响应 最有效,最快的解决办法 就是:重启电脑 或者 关闭电脑,在开机,找到MySQL安装目录, ...

  9. Leetcode刷题第三天-贪心-双指针

    738:单调递增 链接:738. 单调递增的数字 - 力扣(LeetCode) 嘶~介个介个恶心心,从后往前遍历,前一个数比当前数大,前一个数-1,当前数变为9 需要注意的是,保证每个9后面全是9 1 ...

  10. KB0004.如何进行DoraCloud版本升级?

    升级过程为: 1).现有版本,进入维护模式,导出系统数据.    2).记录现当前版本DoraCloud VM 的IP地址,子网掩码.网关.DNS信息,将VM关机. 3).安装新版本DoraCloud ...