Countdown/Countup Timer Hooks For Vue 3

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:

Generic Timer Hook For Vue 3

Changelog:

v1.0.82 (05/15/2023)

  • Update

Download Details:

Author: riderx

Live Demo: https://riderx.github.io/vue-timer-hook/

Download Link: https://github.com/riderx/vue-timer-hook/archive/refs/heads/main.zip

Official Website: https://github.com/riderx/vue-timer-hook

Install & Download:

# Yarn
$ yarn add vue-timer-hook

# NPM
$ npm i vue-timer-hook --save

One Response

  1. santamaria November 12, 2021

Add Comment