session.js 730 Bytes
// setSession
export function setSession (key, value, time) {
    time = time ? Date.parse(new Date()) + time * 1000 * 60 : null
    let params = {
        value: value,
        time: time ? time : null
    }
    window.sessionStorage.setItem(key, JSON.stringify(params))
}
// getSession
export function getSession (key) {
    let params = window.sessionStorage.getItem(key)
    if (!params) return null
    params = JSON.parse(params)
    if (params.time) {
        if (params.time > Date.parse(new Date)) {
            return params.value
        }
        window.sessionStorage.removeItem(key)
        return null
    }
    return params.value
}
export function removeSession (key) {
    return sessionStorage.removeItem(key)
}