Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

save in local storage with expiration

function ls_support(){
    return "localStorage" in window&&window["localStorage"]!==null;
}

function lsset(key,val,exp){
    if(ls_support()){
        if(!exp) exp=600;// = 10 minutes Default
        localStorage[key]=
            JSON.stringify({
                "val":val,
                "exp":~~((new Date()).getTime()/1000)+exp
            });
    }
}

function lsget(key){
    if(ls_support()){
        str=localStorage[key];
        if("undefined"!=typeof(str)&&str.length){
            try{// is json or not
                json=JSON.parse(str);
            }catch(e){// if variable not set via lsset func
                //json.exp=false;// will return null
                return str;// will return original variable
            }
            if(json.exp){// variable setted via lsset func
                if(~~((new Date()).getTime()/1000)>json.exp){// expired
                    delete localStorage[key];
                }else{
                    return json.val;
                }
            }
        }
    }
    return null;
}
Comment

save in local storage with expiration

function setWithExpiry(key, value, ttl) {
	const now = new Date()

	// `item` is an object which contains the original value
	// as well as the time when it's supposed to expire
	const item = {
		value: value,
		expiry: now.getTime() + ttl,
	}
	localStorage.setItem(key, JSON.stringify(item))
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: ts(2749) react testing library 
Javascript :: vue directive parameter 
Javascript :: vanilla js game loop 
Javascript :: position of element related to parent div in jquery 
Javascript :: 4.3.2. Evaluating Variables¶ 
Javascript :: 5.1.3. Boolean Expressions¶ 
Javascript :: Checking equality with Promise.resolve vs async return 
Javascript :: jssor js slider next prev arrows position 
Javascript :: replicate component did update hooks 
Javascript :: Set initial state on react-native-router-flex 
Javascript :: moment format escape 
Javascript :: javascript average of float array 
Javascript :: append dynamica html in jsx react 
Javascript :: tableexport dates 
Javascript :: movie-trailer usage 
Javascript :: clima 
Javascript :: how to render first three element using map 
Javascript :: javascript get local timezone 
Javascript :: route edit button in laravel ajax 
Javascript :: send email using javascript and mailtrap 
Javascript :: client.guilds foreach 
Javascript :: react usestate vs variable 
Javascript :: auto closing not working jsx 
Javascript :: javascript to python converter online 
Javascript :: close all function of react in vscode mac 
Javascript :: modal nodejs 
Javascript :: How To: Build a Live Broadcasting Web App 
Javascript :: how to create a new react app 
Javascript :: how to stop component to render multiple time 
Javascript :: angular cannot access event.target.value of input element using $event 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =