UNPKG

@stackoverfloweth/vue-compositions

Version:

A collection of reusable vue compositions.

41 lines 1.13 kB
import { ref } from 'vue'; import { tryOnScopeDispose } from '../utilities/tryOnScopeDispose'; export function useNow({ immediate = true, interval = 0, } = {}) { const intervalRef = ref(interval); const response = ref(getNow()); let id = null; function getNow() { if (intervalRef.value === 0) { return new Date(); } const time = new Date().getTime(); const nearest = Math.round(time / intervalRef.value) * intervalRef.value; return new Date(nearest); } function update() { const now = getNow(); if (response.value.getTime() !== now.getTime()) { response.value = now; } id = window.requestAnimationFrame(update); } function pause() { if (id) { window.cancelAnimationFrame(id); } id = null; } function resume() { id = window.requestAnimationFrame(update); } if (immediate) { resume(); } tryOnScopeDispose(pause); return { now: response, resume, pause, }; } //# sourceMappingURL=useNow.js.map