当前位置:网站首页>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);
}
};
}
边栏推荐
- POC——DVWA‘s File Inclusion
- 【C】 Beam calculator
- 学习C语言第7天
- MD5 password encryption
- uniapp 使用uview实现折叠面板
- mysql数据库实验实训6,数据视图(详细)
- Cve-2019-14234 Django jsonfield SQL injection vulnerability
- Uni app conditional compilation ifdef ENDIF compatible with multiple terminals
- pygame-飞机大战1.0(步骤+窗口无响应问题)
- [2022 10th Teddy Cup Challenge] Title A: complete version of pest identification (general idea. Detailed process and code and results CSV in compressed package)
猜你喜欢
随机推荐
STL容器——queue与deque的基本操作
ThreadLocal thread safety example and its principle
(精讲)Es6 剩余参数,ES6内置对象,模板字符串内容(详例宝典)及灵活运用项目的实战案例
小程序云开发 上传图片到云存储
First training notes of moderlarts
【Es6】快速实现用户信息打印至页面中
STL容器——set集合的应用
6S参数
Logic of image uploading
学习C语言第二天
C语言 带你 手撕 通讯录
实习项目2-主页配置-我的数据模块
学习C语言第8天
【2022第十届‘泰迪杯’挑战赛】A题:害虫识别完整版(大致思路。详细过程和代码以及结果csv在压缩包中)
获取URL参数的两种方法及location对象的各项获取方式
【LeetCode——编程能力入门第一天】基本数据类型[在区间范围内统计奇数数目/去掉最低工资和最高工资后的工资平均值)
硬核结构体,暴力解读
微信小程序云开发使用方法-1
Use of transactions - Django, SQL tools
Es6最新常用知识宝典(能够帮助你解决面试题困惑,编写程序中出现的问题等)








