1.0.84 • Published 12 months ago

vue-timer-hook v1.0.84

Weekly downloads
-
License
MIT
Repository
-
Last release
12 months ago

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

keyTypeRequiredDescription
expiryTimestampnumber(timestamp)YESthis will define for how long the timer will be running
autoStartbooleanNoflag to decide if timer should start automatically, by default it is set to true

Values

keyTypeDescription
secondsnumberseconds value
minutesnumberminutes value
hoursnumberhours value
daysnumberdays value
isRunningbooleanflag to indicate if timer is running or not
pausefunctionfunction to be called to pause timer
startfunctionfunction if called after pause the timer will continue based on original expiryTimestamp
resumefunctionfunction if called after pause the timer will continue countdown from last paused state
restartfunctionfunction 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

keyTypeRequiredDescription
autoStartbooleanNoif set to true stopwatch will auto start, by default it is set to false
offsetTimestampnumberNothis 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

keyTypeDescription
secondsnumberseconds value
minutesnumberminutes value
hoursnumberhours value
daysnumberdays value
isRunningbooleanflag to indicate if stopwatch is running or not
startfunctionfunction to be called to start/resume stopwatch
pausefunctionfunction to be called to pause stopwatch
resetfunctionfunction 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

keyTypeRequiredDescription
formatstringNoif set to 12-hour time will be formatted with am/pm

Values

keyTypeDescription
secondsnumberseconds value
minutesnumberminutes value
hoursnumberhours value
ampmstringam/pm value if 12-hour format is used

Credit

Inspired by react-timer-hook

1.0.62

1 year ago

1.0.61

1 year ago

1.0.60

1 year ago

1.0.66

1 year ago

1.0.65

1 year ago

1.0.64

1 year ago

1.0.63

1 year ago

1.0.69

1 year ago

1.0.68

1 year ago

1.0.67

1 year ago

1.0.73

1 year ago

1.0.72

1 year ago

1.0.71

1 year ago

1.0.70

1 year ago

1.0.77

1 year ago

1.0.76

1 year ago

1.0.75

1 year ago

1.0.74

1 year ago

1.0.79

1 year ago

1.0.78

1 year ago

1.0.80

1 year ago

1.0.84

12 months ago

1.0.83

12 months ago

1.0.82

12 months ago

1.0.81

12 months ago

1.0.55

1 year ago

1.0.54

1 year ago

1.0.53

1 year ago

1.0.52

1 year ago

1.0.59

1 year ago

1.0.58

1 year ago

1.0.57

1 year ago

1.0.56

1 year ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.29

1 year ago

1.0.28

1 year ago

1.0.27

1 year ago

1.0.33

1 year ago

1.0.32

1 year ago

1.0.31

1 year ago

1.0.30

1 year ago

1.0.37

1 year ago

1.0.36

1 year ago

1.0.35

1 year ago

1.0.34

1 year ago

1.0.40

1 year ago

1.0.44

1 year ago

1.0.43

1 year ago

1.0.42

1 year ago

1.0.41

1 year ago

1.0.48

1 year ago

1.0.47

1 year ago

1.0.46

1 year ago

1.0.45

1 year ago

1.0.49

1 year ago

1.0.51

1 year ago

1.0.50

1 year ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.11

2 years ago

1.0.0

2 years ago

0.0.22

2 years ago

0.0.21

3 years ago

0.0.20

3 years ago

0.0.19

3 years ago

0.0.18

3 years ago

0.0.15

3 years ago

0.0.16

3 years ago

0.0.17

3 years ago

0.0.11

3 years ago

0.0.12

3 years ago

0.0.13

3 years ago

0.0.14

3 years ago

0.0.10

3 years ago

0.0.8

3 years ago

0.0.2

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.1

3 years ago