当前位置:网站首页>cookie是否有效时间限定?如何设置cookie?手把手教你设置
cookie是否有效时间限定?如何设置cookie?手把手教你设置
2022-07-17 05:05:00 【昔日_少年】
1.cookie是否有效时间限定?
cookie是有有效时间的,并且可以自定义有效时间
2.如何设置cookie?
这里我是直接封装起来,直接贴代码,不懂可以私信!!!
// 设置cookie
static setCookie(key, value, expiremHours) {
var exdate = new Date();
exdate.setTime(exdate.getTime() + expiremHours * 60 * 60 * 1000);
document.cookie =
key +
"=" +
escape(value) +
(expiremHours == null ? "" : ";expires=" + exdate.toUTCString());
}
// 读取cookie
static getCookie(key) {
if (document.cookie.length > 0) {
var c_start = document.cookie.indexOf(key + "=");
if (c_start != -1) {
c_start = c_start + key.length + 1;
var c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) c_end = document.cookie.length;
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
}
// 删除cookie
static delCookie(key) {
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = this.getCookie(key);
if (cval != null) {
document.cookie = key + "=" + cval + ";expires=" + exp.toUTCString();
}
}
// 节流
static throttle(callBack, time) {
let timer = null;
//timer状态要常驻内存,这里做了一个闭包
return function() {
if (!timer) {
timer = setTimeout(() => {
callBack();
timer = null;
}, time);
}
};
}
边栏推荐
- (精讲)Es6 剩余参数,ES6内置对象,模板字符串内容(详例宝典)及灵活运用项目的实战案例
- 一个问题的探讨
- Class object automatic injection attribute operation tool
- STL容器——map的基本操作
- 读论文《Learning to Measure Changes: Fully Convolutional Siamese Metric Networks for Scene Change Detec》
- IDL 读取葵花8(Himawari-8)HSD数据
- 【LeetCode——编程能力入门第二天】运算符(位1的个数/整数的各位积和之差)
- Message converter (JSON)
- 645. 错误的集合
- vscode终端无法使用解决的办法
猜你喜欢

First training notes of moderlarts

第十届泰迪杯数据挖掘挑战赛A题害虫识别YOLOv5模型代码(已跑通,原创作品,持续更新)

实习项目2-主页配置-我的数据模块

Teddy Cup title a full version optimization update (4/23)

PyGame aircraft War 1.0 (step + window no response problem)

SMS verification test without signature template audit
![[2022 10th Teddy Cup Challenge] Title A: complete version of pest identification (general idea. Detailed process and code and results CSV in compressed package)](/img/e6/beea0bb0a9f4b41206c6fcb130fdfd.png)
[2022 10th Teddy Cup Challenge] Title A: complete version of pest identification (general idea. Detailed process and code and results CSV in compressed package)

About the current response, the method getoutputstream() has been called

Mongo DB aggregate operations and indexes

Harmonyos fourth training notes
随机推荐
PyGame installation -requirement already satisfied
02_ Movie recommendation (contentbased)_ User portrait
这么6的刷题网站你不会没听说过吧?你已经out 了?
computed和watch的区别
【C】张梁计算器
决策树原理和案例应用-泰坦尼克号生存预测
小程序云开发表单提交并在页面中获取数据
Install MySQL
Three high concurrency methods to realize I++
pygame安装-Requirement already satisfied问题
Harmonyos third training notes
【LeetCode——编程能力入门第二天】运算符(位1的个数/整数的各位积和之差)
模拟库函数
First training notes of moderlarts
基于RTX30显卡的ArcGIS Pro2.8深度学习环境配置
PyGame aircraft War 1.0 (step + window no response problem)
百度地图 实现 热力图
[p5.js] simulated fireworks effect - interactive media design assignment
小程序editor富文本编辑使用及rich-text解析富文本
pygame-飞机大战1.0(步骤+窗口无响应问题)