Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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))
}
Source by www.sohamkamani.com #
 
PREVIOUS NEXT
Tagged: #save #local #storage #expiration
ADD COMMENT
Topic
Name
5+9 =