Vue function that returns isReady
value as true
after a specified ms
amount of time.
function useTimeout(
ms?: number,
runOnMount?: boolean
): {
isReady: Ref<boolean | null>;
start: () => void;
stop: () => void;
}
ms: number
how many milliseconds to wait before the timer is completedrunOnMount: 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 to stop the timer
<template>
<div>
<p>Timer status: {{ isReady ? 'Called!' : 'Pending...' }}</p>
<button @click="start">Reset Timer</button>
<button @click="stop">Stop Timer</button>
</div>
</template>
<script lang="ts">
import Vue from 'vue'
import { useTimeout } from 'vue-use-kit'
export default Vue.extend({
name: 'UseTimeoutDemo',
setup() {
const timerDuration = 3000
const { isReady, start, stop } = useTimeout(timerDuration)
return { isReady, start, stop }
}
})
</script>