Created
April 11, 2021 04:43
-
-
Save cyberdev/d4d1919ac966a2eb392aa0aa2ffeb8f4 to your computer and use it in GitHub Desktop.
Save visited data using localStorage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const itemStr = localStorage.getItem('visit-data') | |
let items = []; | |
if (itemStr) { | |
items = JSON.parse(itemStr); | |
} | |
//delete if exists | |
let item = items.find(o => o.code === code); | |
if(item){ | |
items = items.filter(row => row.code !== code); | |
} | |
//push new data | |
const exp = new Date(); | |
exp.setDate(exp.getDate() + 1); | |
exp.setHours(3,0,0,0); | |
items.push({ | |
code: code, | |
expiry: exp.getTime() | |
}) | |
localStorage.setItem('visit-data', JSON.stringify(items)); | |
//get data demo | |
const itemStr = localStorage.getItem('visit-data') | |
// if the item doesn't exist, return false | |
if (!itemStr) { | |
itemStr = false; | |
} | |
const now = new Date() | |
const items = JSON.parse(itemStr); | |
let item = items.find(o => o.code === code); | |
var isrecent = false | |
if(item){ | |
if(item.expiry > now.getTime()){ | |
isrecent = true | |
}else{ | |
const newitems = items.filter(row => row.code !== item.code); | |
localStorage.setItem('visit-data', JSON.stringify(newitems)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment