If you are utilizing the composition API, here is a solution for your scenario.
One option is to incorporate the useTimeAgo
composable from VueUse. This will provide you with a formatted string that updates at a specified interval.
Here's an example to demonstrate how it works:
import { useTimeAgo } from '@vueuse/core'
const eventDate = new Date(2022, 0, 1)
const timeAgo = useTimeAgo(eventDate, { updateInterval: 30_000 }) // Updates every 30 seconds
It's worth noting that you cannot set requestAnimationFrame
as the interval like you can with the useNow
composable. However, according to Antfu's explanation on GitHub:
useTimeAgo
doesn't require high accuracy in timing, so using requestAnimationFrame
would be inefficient and increase bundle size unnecessarily.
For more information, check out useNow
on VueUse.