Giter VIP home page Giter VIP logo

vue-timer-hook's Introduction

vue-timer-hook

Vue timer hook is a custom vue 3 hook, built to handle timer, stopwatch, and time logic/state in your vue component.

  1. useTimer: Timers (countdown timer)
  2. useStopwatch: Stopwatch (count up timer)
  3. useTime: Time (return current time)

Setup

yarn add vue-timer-hook OR npm install vue-timer-hook


useTimer - Demo

Example

<template>
    <div>
        <h1>vue-timer-hook </h1>
        <p>Timer Demo</p>
        <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>


<script setup lang="ts">
import {  watchEffect, onMounted } from "vue";
import { useTimer } from 'vue-timer-hook';

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')
    }
  })
})
</script>

Settings

key Type Required Description
expiryTimestamp number(timestamp) YES this will define for how long the timer will be running
autoStart boolean No flag to decide if timer should start automatically, by default it is set to true

Values

key Type Description
seconds number seconds value
minutes number minutes value
hours number hours value
days number days value
isRunning boolean flag to indicate if timer is running or not
pause function function to be called to pause timer
start function function if called after pause the timer will continue based on original expiryTimestamp
resume function function if called after pause the timer will continue countdown from last paused state
restart function function to restart timer with new expiryTimestamp, accept 2 arguments first is the new expiryTimestamp of type number(timestamp) and second is autoStart of type boolean to decide if it should automatically start after restart or not, default is true

useStopwatch - Demo

Example

<template>
    <div>
        <h1>vue-timer-hook </h1>
        <p>Stopwatch Demo</p>
        <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>


<script setup lang="ts">
import { defineComponent } from "vue";
import { useStopwatch } from 'vue-timer-hook';

const autoStart = true;
const stopwatch = useStopwatch(autoStart);
</script>

Settings

key Type Required Description
autoStart boolean No if set to true stopwatch will auto start, by default it is set to false
offsetTimestamp number No this will define the initial stopwatch offset example: const stopwatchOffset = new Date(); stopwatchOffset.setSeconds(stopwatchOffset.getSeconds() + 300); this will result in a 5 minutes offset and stopwatch will start from 0:0:5:0 instead of 0:0:0:0

Values

key Type Description
seconds number seconds value
minutes number minutes value
hours number hours value
days number days value
isRunning boolean flag to indicate if stopwatch is running or not
start function function to be called to start/resume stopwatch
pause function function to be called to pause stopwatch
reset function function to be called to reset stopwatch to 0:0:0:0, you can also pass offset parameter to this function to reset stopwatch with offset, similar to how offsetTimestamp will offset the initial stopwatch time, this function will accept also a second argument which will decide if stopwatch should automatically start after reset or not default is true

useTime - Demo

Example

<template>
    <div>
        <h1>vue-timer-hook </h1>
        <p>Current Time Demo</p>
        <div>
            <span>{{time.hours}}</span>:<span>{{time.minutes}}</span>:<span>{{time.seconds}}</span><span>{{time.ampm}}</span>
        </div>
    </div>
</template>


<script lang="ts">
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,
     };
  },
});
</script>

Settings

key Type Required Description
format string No if set to 12-hour time will be formatted with am/pm

Values

key Type Description
seconds number seconds value
minutes number minutes value
hours number hours value
ampm string am/pm value if 12-hour format is used

Credit

Inspired by react-timer-hook

vue-timer-hook's People

Contributors

batmanppc avatar dependabot[bot] avatar github-actions[bot] avatar lookfirst avatar metachris avatar renovate[bot] avatar riderx avatar stevenblack avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

vue-timer-hook's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): replace dependency rollup-plugin-terser with @rollup/plugin-terser ^0.1.0
  • chore(deps): update dependency @types/webpack to v5.28.5
  • chore(deps): update dependency nightwatch to v2.6.25
  • chore(deps): update dependency ts-node to v10.9.2
  • chore(deps): update dependency vite to v2.9.18
  • chore(deps): update commitlint monorepo to v17.8.1 (@commitlint/cli, @commitlint/config-conventional)
  • chore(deps): update dependency @types/node to v18.19.31
  • chore(deps): update dependency @vue/test-utils to v2.4.5
  • chore(deps): update dependency chalk to v5.3.0
  • chore(deps): update dependency css-loader to v6.11.0
  • chore(deps): update dependency dotenv to v16.4.5
  • chore(deps): update dependency lint-staged to v13.3.0
  • chore(deps): update dependency rollup-plugin-typescript2 to v0.36.0
  • chore(deps): update dependency vue-tsc to v1.8.27
  • chore(deps): update jest monorepo (@types/jest, jest)
  • chore(deps): update vue monorepo to v3.4.26 (@vue/compiler-sfc, @vue/server-renderer, vue)
  • chore(deps): update actions/checkout action to v4
  • chore(deps): update actions/setup-node action to v4
  • chore(deps): update commitlint monorepo to v19 (major) (@commitlint/cli, @commitlint/config-conventional)
  • chore(deps): update dependency @rollup/plugin-alias to v5
  • chore(deps): update dependency @rollup/plugin-commonjs to v25
  • chore(deps): update dependency @types/jsdom to v21
  • chore(deps): update dependency @types/node to v20
  • chore(deps): update dependency @vitejs/plugin-vue to v5
  • chore(deps): update dependency chromedriver to v124
  • chore(deps): update dependency conventional-changelog-cli to v4
  • chore(deps): update dependency css-loader to v7
  • chore(deps): update dependency gh-pages to v6
  • chore(deps): update dependency husky to v9
  • chore(deps): update dependency lint-staged to v15
  • chore(deps): update dependency nightwatch to v3
  • chore(deps): update dependency prettier to v3
  • chore(deps): update dependency rollup to v4
  • chore(deps): update dependency typescript to v5
  • chore(deps): update dependency vite to v5
  • chore(deps): update dependency vitepress to v1
  • chore(deps): update dependency vue-tsc to v2
  • chore(deps): update js-devtools/npm-publish action to v3
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/bump_version.yml
  • actions/checkout v3
  • pnpm/action-setup v2
  • actions/setup-node v3
  • actions/checkout v3
  • pnpm/action-setup v2
  • actions/setup-node v3
.github/workflows/release-tag.yml
  • actions/checkout v3
  • pnpm/action-setup v2
  • actions/setup-node v3
  • JS-DevTools/npm-publish v1
.github/workflows/test.yml
  • actions/checkout v3
  • pnpm/action-setup v2
  • actions/setup-node v3
npm
package.json
  • @commitlint/cli ^17.1.1
  • @commitlint/config-conventional ^17.1.0
  • @microsoft/api-extractor 7.34.9
  • @rollup/plugin-alias 4.0.4
  • @rollup/plugin-commonjs 23.0.7
  • @rollup/plugin-node-resolve 15.0.2
  • @rollup/plugin-replace 4.0.0
  • @sucrase/jest-plugin 2.2.1
  • @types/jest 29.2.5
  • @types/jsdom 20.0.1
  • @types/node ^18.7.13
  • @types/webpack ^5.28.0
  • @types/webpack-env 1.18.0
  • @vitejs/plugin-vue 2.3.4
  • @vue/compiler-sfc ^3.2.31
  • @vue/server-renderer 3.2.47
  • @vue/test-utils ^2.0.0-rc.18
  • axios 1.3.4
  • brotli ^1.3.2
  • browserstack-local 1.5.2
  • chalk 5.2.0
  • chromedriver 105.0.1
  • codecov 4.0.0-0
  • connect-history-api-fallback ^2.0.0
  • conventional-changelog-cli 2.2.2
  • css-loader 6.7.4
  • dotenv 16.0.3
  • faked-promise ^2.2.2
  • gh-pages ^4.0.0
  • html-webpack-plugin 5.5.1
  • husky ^8.0.1
  • jest 29.3.1
  • jest-mock-warn ^1.1.0
  • lint-staged 13.1.2
  • mockdate ^3.0.5
  • nightwatch 2.6.21
  • nightwatch-helpers ^1.2.0
  • prettier 2.8.7
  • rollup 2.79.1
  • rollup-plugin-analyzer ^4.0.0
  • rollup-plugin-terser ^7.0.2
  • rollup-plugin-typescript2 0.34.1
  • selenium-server ^3.141.59
  • serve-handler ^6.1.3
  • ts-node ^10.9.1
  • typescript 4.9.5
  • vite ^2.8.4
  • vitepress 0.22.4
  • vue 3.2.47
  • vue-tsc 1.0.22
  • yorkie ^2.0.0
  • vue ^3.2.0

  • Check this box to trigger a request for Renovate to run again on this repository

style

Hi, is there any way to change the color of timer?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.