js cookie实现在一天内只执行一次
function versionExpireTime(key,method) {
var v = getCookie(key);
if (!v) {
method(true); //外面传进来的callback,方便告诉外部是否过期
//获取第二天凌晨到当前时间的秒数
var time_sec = 24 * 60 * 60 - (new Date().getHours() * 60 * 60 + new Date().getMinutes() * 60 + new Date().getSeconds());
setCookie(key, "1", time_sec);
}
}
function dosome(msg){
console.log(msg);
}
//写cookies
function setCookie(name, value, second) {
if (!second) {
second = 7 * 24 * 60 * 60;//默认7天
}
var exp = new Date();
exp.setTime(exp.getTime() + second * 1000);
document.cookie = name + "=" + encodeURI(value) + ";expires=" + exp.toGMTString() + ";path=/";
}
//读取cookies
function getCookie(name) {
if (document.cookie.length > 0) {
let c_start = document.cookie.indexOf(name + "=");//获取字符串的起点
if (c_start != -1) {
c_start = c_start + name.length + 1;//获取值的起点
let c_end = document.cookie.indexOf(";", c_start);//获取结尾处
if (c_end == -1) c_end = document.cookie.length;//如果是最后一个,结尾就是cookie字符串的结尾
return decodeURI(document.cookie.substring(c_start, c_end));//截取字符串返回
}
}
return '';
}
export default versionExpireTime;
版权声明:本文由Web学习之路发布,如需转载请注明出处。