Vue function that calls given callback repeatedly on a fixed time delay.
function useIntervalFn(
callback: Function,
ms?: number,
runOnMount?: boolean
): {
isRunning: Ref<boolean>;
start: () => void;
stop: () => void;
};
callback: Function
the function to call for each interval finishesms: number
how many milliseconds to wait before running the callback functionrunOnMount: boolean
whether to run the interval on mount,true
by default
isRunning: Ref<boolean>
this value istrue
if the interval is running,false
otherwisestart: Function
the function used for starting the intervalstop: Function
the function used for stopping the interval
<template>
<div>
<p>callbackCounter: {{ callbackCounter }}</p>
<button @click="start" v-if="!isRunning">Start Interval</button>
<button @click="stop" v-else>Stop Interval</button>
</div>
</template>
<script lang="ts">
import Vue from 'vue'
import { ref } from '@src/api'
import { useIntervalFn } from 'vue-use-kit'
export default Vue.extend({
name: 'UseIntervalFnDemo',
setup() {
const ms = 300
const callbackCounter = ref(0)
const animHandler = () => {
callbackCounter.value = callbackCounter.value + 1
}
const { isRunning, start, stop } = useIntervalFn(animHandler, ms)
return { isRunning, start, stop, callbackCounter }
}
})
</script>