Vue function that returns the total elapsed time excluding paused intervals.
You can optionally specify how many times in a second should the elapsed
time update by specifying the fps
value.
type TFps = number | Ref<number>;
function useRaf(
fps?: TFps,
runOnMount?: boolean
): {
isRunning: Ref<boolean>;
elapsed: Ref<number>;
start: () => void;
stop: () => void;
}
fps: number | Ref<number>
the amount of times per second that the elapsed time should update. Please note that when a value greater or equal to60
is defined, thefps
logic will be skipped completely therefore the elapsed time will update at full speed. By default the value is set to60
so it will indeed update at full speedrunOnMount: boolean
whether to run the Raf loop on mount,true
by default. Iffalse
, you'll have to start the Raf with thestart
function
isRunning: Ref<boolean>
the Raf statusfalse
when the Raf loop is pausedtrue
when the Raf loop is running
elapsed: Ref<number>
the total elapsed time excluding paused intervalsstart: Function
the function used for starting the Raf loopstop: Function
the function used for stopping the Raf loop
<template>
<div>
<div>Elapsed time: {{ elapsed }}</div>
<div><button @click="start">Start loop</button></div>
<div><button @click="stop">Stop loop</button></div>
</div>
</template>
<script lang="ts">
import Vue from 'vue'
import { useRaf } from 'vue-use-kit'
export default Vue.extend({
name: 'UseRafDemo',
setup() {
const fps = 60
const { elapsed, start, stop } = useRaf(fps)
return {
elapsed
start,
stop
}
}
})
</script>