函数封装
//基于localStorage本地存储
var store={
set:function(name, value, day) { // 设置
let d = new Date()
let time = 0
day = (typeof(day) === 'undefined' || !day) ? 1 : day // 时间,默认存储1天
time = d.setHours(d.getHours() + (24 * day)) // 毫秒
localStorage.setItem(name, JSON.stringify({
data: value,
time: time
}))
},
get:function(name) { // 获取
let data = localStorage.getItem(name)
if (!data) {
return null
}
let obj = JSON.parse(data)
if (new Date().getTime() > obj.time) { // 过期
localStorage.removeItem(name)
return null
} else {
return obj.data
}
},
clear:function(name) { // 清空
if (name) { // 删除键为name的缓存
localStorage.removeItem(name)
} else { // 清空全部
localStorage.clear()
}
},
};
store.set('dyh','内容',2);//设置存储有效期两天
console.log(store.get('dyh'));
store.clear();
闭包方式
;(function() {
//基于localStorage本地存储
const store={
set:function(name, value, day) { // 设置
let d = new Date()
let time = 0
day = (typeof(day) === 'undefined' || !day) ? 1 : day // 时间,默认存储1天
time = d.setHours(d.getHours() + (24 * day)) // 毫秒
localStorage.setItem(name, JSON.stringify({
data: value,
time: time
}))
},
get:function(name) { // 获取
let data = localStorage.getItem(name)
if (!data) {
return null
}
let obj = JSON.parse(data)
if (new Date().getTime() > obj.time) { // 过期
localStorage.removeItem(name)
return null
} else {
return obj.data
}
},
clear:function(name) { // 清空
if (name) { // 删除键为name的缓存
localStorage.removeItem(name)
} else { // 清空全部
localStorage.clear()
}
},
}
const fun={
store:store, //基于localStorage本地存储
}
if (typeof module !== "undefined" && module.exports) module.exports = fun;
if (typeof define === "function") define(function() { return fun; });
window.$fun=fun
})();
ES6 import/export
//基于localStorage本地存储
export const store={
set:function(name, value, day) { // 设置
let d = new Date()
let time = 0
day = (typeof(day) === 'undefined' || !day) ? 1 : day // 时间,默认存储1天
time = d.setHours(d.getHours() + (24 * day)) // 毫秒
localStorage.setItem(name, JSON.stringify({
data: value,
time: time
}))
},
get:function(name) { // 获取
let data = localStorage.getItem(name)
if (!data) {
return null
}
let obj = JSON.parse(data)
if (new Date().getTime() > obj.time) { // 过期
localStorage.removeItem(name)
return null
} else {
return obj.data
}
},
clear:function(name) { // 清空
if (name) { // 删除键为name的缓存
localStorage.removeItem(name)
} else { // 清空全部
localStorage.clear()
}
},
};
ES6 export default
//基于localStorage本地存储
const store={
set:function(name, value, day) { // 设置
let d = new Date()
let time = 0
day = (typeof(day) === 'undefined' || !day) ? 1 : day // 时间,默认存储1天
time = d.setHours(d.getHours() + (24 * day)) // 毫秒
localStorage.setItem(name, JSON.stringify({
data: value,
time: time
}))
},
get:function(name) { // 获取
let data = localStorage.getItem(name)
if (!data) {
return null
}
let obj = JSON.parse(data)
if (new Date().getTime() > obj.time) { // 过期
localStorage.removeItem(name)
return null
} else {
return obj.data
}
},
clear:function(name) { // 清空
if (name) { // 删除键为name的缓存
localStorage.removeItem(name)
} else { // 清空全部
localStorage.clear()
}
},
};
export default {
store:store,
}