Skip to content

Instantly share code, notes, and snippets.

@8ctopotamus
Created July 28, 2023 14:13
Show Gist options
  • Save 8ctopotamus/167b46427d55c3a7019ebc7f592e294f to your computer and use it in GitHub Desktop.
Save 8ctopotamus/167b46427d55c3a7019ebc7f592e294f to your computer and use it in GitHub Desktop.
Detecting if data is stale after a certain time period.
// const savedTimestamp = 1690509844000 // saved over 12 hours ago in localStorage
const savedTimestamp = 1690553044000 // saved under 12 hours ago in localStorage
const twelveHoursDifference = 43200000 // 12 hours in milliseconds
const now = new Date().getTime() // current timestamp
const isDataStale = (now, timestamp, difference) => now > timestamp + difference
const shouldRefresh = isDataStale(now, savedTimestamp, twelveHoursDifference)
console.log(shouldRefresh)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment