Vue function that calls given callback after a specified ms
amount of time.
function useTimeoutFn(
callback: Function,
ms?: number,
runOnMount?: boolean
): {
isReady: Ref<boolean | null>;
start: () => void;
stop: () => void;
}
callback: Function
the function to call when the timer finishesms: number
how many milliseconds to wait before running the callback functionrunOnMount: boolean
whether to run the timeout on mount,true
by default
isReady: Ref<boolean | null>
the timer statusfalse
when the timer is executingtrue
when the timer is completednull
when the timer is idle
start: Function
the function used for starting or resetting the timerstop: Function
the function used for stopping the timer
<template>
<div>
<p>Timer status: {{ isReady ? 'Called!' : 'Pending...' }}</p>
<p>Timeout Callback msg: {{ timerFnMsg }}</p>
<button @click="start">Reset Timer</button>
<button @click="stop">Stop Timer</button>
</div>
</template>
<script lang="ts">
import Vue from 'vue'
import { ref } from '@src/api'
import { useTimeoutFn } from 'vue-use-kit'
export default Vue.extend({
name: 'UseTimeoutFnDemo',
setup() {
const timerFnMsg = ref('Timer not completed')
const timerDuration = 3000
const timerHandler = () => {
timerFnMsg.value = 'Timer completed!'
}
const { isReady, start, stop } = useTimeoutFn(
timerHandler,
timerDuration
)
return { timerFnMsg, isReady, start, stop }
}
})
</script>