Skip to content

v-countdown

Display a countdown timer to a target time. Supports multiple formats and completion callbacks.

Since: 1.3.0

Usage

Basic

vue
<template>
  <span v-countdown="targetDate"></span>
</template>

<script setup>
const targetDate = new Date('2026-12-31T00:00:00')
</script>

With Timestamp

vue
<template>
  <span v-countdown="Date.now() + 60000"></span>
</template>

With Options

vue
<template>
  <span v-countdown="{
    target: targetDate,
    format: 'dd:hh:mm:ss',
    onComplete: handleComplete
  }"></span>
</template>

API

Types

typescript
interface CountdownOptions {
  target: Date | number | string
  format?: string | ((time: CountdownTime) => string)
  interval?: number // default: 1000
  onComplete?: () => void
  onTick?: (time: CountdownTime) => void
  autoStart?: boolean // default: true
}

interface CountdownTime {
  days: number
  hours: number
  minutes: number
  seconds: number
  milliseconds: number
  total: number
}

Options

OptionTypeDefaultDescription
targetDate | number | string-Target time (required)
formatstring | Function'hh:mm:ss'Display format or custom function
intervalnumber1000Update interval in milliseconds
onComplete() => void-Callback when countdown ends
onTick(time: CountdownTime) => void-Callback on each tick
autoStartbooleantrueAuto-start countdown

Format Placeholders

PlaceholderDescription
ddDays (2 digits)
hhHours (2 digits)
mmMinutes (2 digits)
ssSeconds (2 digits)
SSSMilliseconds (3 digits)

Examples

Sale Countdown

vue
<template>
  <div class="sale-banner">
    Sale ends in: <span v-countdown="{ target: saleEnd, format: 'dd:hh:mm:ss' }"></span>
  </div>
</template>

Custom Format

vue
<template>
  <span v-countdown="{
    target: targetDate,
    format: (time) => `${time.days}d ${time.hours}h ${time.minutes}m`
  }"></span>
</template>

With Completion Callback

vue
<template>
  <span
    v-countdown="{
      target: Date.now() + 5000,
      format: 'ss',
      onComplete: () => completed = true
    }"
  ></span>
  <span v-if="completed">Done!</span>
</template>

Composable API

For programmatic use, you can use the useCountdown composable:

typescript
import { useCountdown, parseTargetTime, calculateTime, formatTime } from 'directix'

const {
  time,
  formatted,
  running,
  paused,
  completed,
  start,
  pause,
  resume,
  reset
} = useCountdown({
  target: targetDate,
  format: 'hh:mm:ss',
  interval: 1000,
  autoStart: true,
  onComplete: () => console.log('Done!'),
  onTick: (time) => console.log('Tick:', time)
})

// Control countdown
start()
pause()
resume()
reset()

UseCountdownOptions

OptionTypeDefaultDescription
targetDate | number | string | Ref-Target time (required)
formatstring | Function | Ref'hh:mm:ss'Display format or custom function
intervalnumber | Ref<number>1000Update interval in milliseconds
autoStartboolean | Ref<boolean>trueAuto-start countdown
onComplete() => void-Callback when countdown ends
onTick(time: CountdownTime) => void-Callback on each tick
showMillisecondsboolean | Ref<boolean>falseShow milliseconds
labelsobject-Custom labels for i18n

UseCountdownReturn

PropertyTypeDescription
timeRef<CountdownTime>Current countdown time object
formattedRef<string>Formatted time string
runningRef<boolean>Whether countdown is running
pausedRef<boolean>Whether countdown is paused
completedRef<boolean>Whether countdown has completed
start() => voidStart the countdown
pause() => voidPause the countdown
resume() => voidResume the countdown
reset() => voidReset the countdown

Example

vue
<script setup>
import { useCountdown } from 'directix'

const targetDate = new Date(Date.now() + 60 * 60 * 1000) // 1 hour from now

const { formatted, running, completed, pause, resume } = useCountdown({
  target: targetDate,
  format: 'hh:mm:ss',
  onComplete: () => console.log('Done!')
})
</script>

<template>
  <div>
    <p>{{ formatted }}</p>
    <button @click="pause" v-if="running">Pause</button>
    <button @click="resume" v-if="!running && !completed">Resume</button>
  </div>
</template>

Code Generator

Quick Code Generator
<template>
  <div v-countdown="{ time: 3600, format: 'mm:ss' }">
    <!-- Display a countdown timer to a target time. Supports multiple formats and completion callbacks. directive -->
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'

// Configure your options here
const options = { time: 3600, format: 'mm:ss' }
</script>

Released under the MIT License.