Install & Download:
# Yarn
$ yarn add vue-timer-hook
# NPM
$ npm i vue-timer-hook --saveDescription:
A custom timer hook for handling countdown timer, stopwatch, and time logic/state in your Vue component.
How to use it:
1. Create a countdown timer using the useTimer hook.
import { defineComponent, watchEffect, onMounted } from "vue";
import { useTimer } from 'vue-timer-hook';
export default defineComponent({
name: "Home",
setup() {
const time = new Date();
time.setSeconds(time.getSeconds() + 600); // 10 minutes timer
const timer = useTimer(time);
const restartFive = () => {
// Restarts to 5 minutes timer
const time = new Date();
time.setSeconds(time.getSeconds() + 300);
timer.restart(time);
}
onMounted(() => {
watchEffect(async () => {
if(timer.isExpired.value) {
console.warn('IsExpired')
}
})
})
return {
timer,
restartFive,
};
},
});<template>
<div>
<div>
<span>{{timer.days}}</span>:<span>{{timer.hours}}</span>:<span>{{timer.minutes}}</span>:<span>{{timer.seconds}}</span>
</div>
<p>{{timer.isRunning ? 'Running' : 'Not running'}}</p>
<button @click="timer.start()">Start</button>
<button @click="timer.pause()">Pause</button>
<button @click="timer.resume()">Resume</button>
<button @click="restartFive()">Restart</button>
</div>
</template>2. Create a stopwatch using the useStopwatch hook.
import { defineComponent } from "vue";
import { useStopwatch } from 'vue-timer-hook';
export default defineComponent({
name: "Home",
setup() {
const autoStart = true;
const stopwatch = useStopwatch(autoStart);
return {
stopwatch,
};
},
});<template>
<div>
<div>
<span>{{stopwatch.days}}</span>:<span>{{stopwatch.hours}}</span>:<span>{{stopwatch.minutes}}</span>:<span>{{stopwatch.seconds}}</span>
</div>
<p>{{stopwatch.isRunning ? 'Running' : 'Not running'}}</p>
<button @click="stopwatch.start()">Start</button>
<button @click="stopwatch.pause()">Pause</button>
<button @click="stopwatch.reset()">Reset</button>
</div>
</template>3. Handle the time state using the useTime hook.
import { defineComponent } from "vue";
import { useTime } from 'vue-timer-hook';
export default defineComponent({
name: "Home",
setup() {
const format = '12-hour'
const time = useTime(format);
return {
time,
};
},
});<template>
<div>
<div>
<span>{{time.hours}}</span>:<span>{{time.minutes}}</span>:<span>{{time.seconds}}</span><span>{{time.ampm}}</span>
</div>
</div>
</template>Preview:

Changelog:
v1.0.82 (05/15/2023)
- Update






I just copy past your stopwatch in a component, and it’s not working.
The value of time stay at 0. But when I console log them the value is changing…