Skip to content


The useTimer composable manages timeouts. It allows you to start, pause, resume, and stop a timer with reactive updates on the remaining time. Ideal for handling asynchronous operations or user interactions with a specified timeout.

The composable provides the following functionality:

  • start: Initiates a timer that decrements the remaining time every second until the timeout is reached. When the timeout is reached, it executes the provided callback function.
  • pause: Pauses the active timer, allowing it to be resumed later from the point it was paused.
  • resume: Resumes the timer from the point it was paused, considering the remaining time.
  • stop: Stops the timer completely, resetting the remaining time to the initial timeout value.
  • remainingTime: A reactive variable that holds the current remaining time in milliseconds. It updates reactively as the timer progresses.

The composable also includes cleanup logic using onBeforeUnmount to ensure that the timer is stopped when the component is unmounted.

This composable can be used in Vue 3 components to handle timeouts in a flexible and reactive manner.


Remaning time: 4000ms

  <MazBtn @click="start">
  <MazBtn color="danger" @click="stop">
  <MazBtn color="warning" @click="pause">
  <MazBtn color="secondary" @click="resume">

<script lang="ts" setup>
  import { useTimer, useToast } from 'maz-ui'

  const toast = useToast()

  const { start, stop, pause, resume, remainingTime } = useTimer({
    timeout: 4000,
    callback: () =>'Timeout end', {
      action: {
        func: start,
        text: 're-start',
        closeToast: true,

Set timeout with start function

Starts the timer with the specified timeout. If the timeout is not provided, it uses the default value of 1000ms.

const { start } = useTimer()



export interface TimerOptions {
   * The time in milliseconds
   * @default 1000
  timeout?: number
   * The callback to execute when the timer is finished
   * @default undefined
  callback?: () => unknown
   * The interval to update the remaining time
   * @default 200
  remainingTimeUpdate?: number