当前位置:首页 > VUE

vue实现倒计时组件

2026-01-07 05:24:49VUE

实现基础倒计时功能

使用 Vue 的 datamethods 定义倒计时逻辑,通过 setInterval 更新剩余时间。

<template>
  <div>{{ formattedTime }}</div>
</template>

<script>
export default {
  data() {
    return {
      remainingTime: 60, // 初始60秒
      timer: null
    };
  },
  computed: {
    formattedTime() {
      const minutes = Math.floor(this.remainingTime / 60);
      const seconds = this.remainingTime % 60;
      return `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
    }
  },
  mounted() {
    this.startTimer();
  },
  methods: {
    startTimer() {
      this.timer = setInterval(() => {
        if (this.remainingTime > 0) {
          this.remainingTime--;
        } else {
          clearInterval(this.timer);
        }
      }, 1000);
    }
  },
  beforeDestroy() {
    clearInterval(this.timer);
  }
};
</script>

支持动态初始时间

通过 props 接收外部传入的初始时间,增强组件复用性。

vue实现倒计时组件

<script>
export default {
  props: {
    initialTime: {
      type: Number,
      default: 60
    }
  },
  data() {
    return {
      remainingTime: this.initialTime
    };
  }
};
</script>

添加暂停与继续功能

扩展方法控制倒计时的暂停和继续。

methods: {
  pauseTimer() {
    clearInterval(this.timer);
  },
  resumeTimer() {
    this.startTimer();
  }
}

自定义时间格式

通过 props 允许用户自定义时间显示格式(如 HH:MM:SS)。

vue实现倒计时组件

props: {
  format: {
    type: String,
    default: 'MM:SS'
  }
},
computed: {
  formattedTime() {
    const hours = Math.floor(this.remainingTime / 3600);
    const minutes = Math.floor((this.remainingTime % 3600) / 60);
    const seconds = this.remainingTime % 60;

    if (this.format === 'HH:MM:SS') {
      return `${hours}:${minutes < 10 ? '0' : ''}${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
    } else {
      return `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
    }
  }
}

事件通知

倒计时结束时触发自定义事件,便于父组件处理逻辑。

methods: {
  startTimer() {
    this.timer = setInterval(() => {
      if (this.remainingTime > 0) {
        this.remainingTime--;
      } else {
        clearInterval(this.timer);
        this.$emit('timeup');
      }
    }, 1000);
  }
}

使用示例

在父组件中调用倒计时组件并传递参数。

<template>
  <Countdown 
    :initial-time="120" 
    format="HH:MM:SS" 
    @timeup="handleTimeup" 
  />
</template>

<script>
import Countdown from './Countdown.vue';
export default {
  components: { Countdown },
  methods: {
    handleTimeup() {
      alert('倒计时结束!');
    }
  }
};
</script>

标签: 倒计时组件
分享给朋友:

相关文章

vue哪个组件实现动画

vue哪个组件实现动画

在Vue中实现动画效果,可以通过以下组件和方法实现: Vue内置动画组件 Vue提供了<transition>和<transition-group>两个内置组件,用于处理进入…

vue实现活动倒计时

vue实现活动倒计时

实现活动倒计时的基本思路 在Vue中实现活动倒计时通常需要获取目标时间与当前时间的差值,通过定时器动态更新剩余时间。核心逻辑包括计算时间差、格式化和动态渲染。 使用计算属性与定时器 定义一个…

vue VUE实现日历组件功能

vue VUE实现日历组件功能

Vue 日历组件实现方法 基础日历结构 使用Vue的模板语法构建日历的HTML结构,通常包含头部(年月显示、切换按钮)和日历表格(星期标题和日期格子)。 <template> <…

vue抽屉组件实现

vue抽屉组件实现

Vue 抽屉组件实现 使用 Element UI 实现 Element UI 提供了现成的抽屉组件 el-drawer,可以快速实现抽屉效果。 安装 Element UI: npm install…

vue 实现toast组件

vue 实现toast组件

实现 Vue Toast 组件的方法 创建基础 Toast 组件 在 Vue 项目中创建一个基础 Toast 组件,通常包含消息内容和显示控制逻辑。 <template> <d…

vue实现盖章组件

vue实现盖章组件

实现Vue盖章组件的步骤 组件结构设计 创建一个Vue组件,包含画布元素和必要的控制按钮。画布用于显示盖章效果,按钮用于触发盖章操作。组件需要接收图片和盖章位置等参数。 <template&g…