当前位置:首页 > 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 折叠组件实现方法 基于 v-show 和事件触发 通过 v-show 控制内容的显示与隐藏,结合点击事件切换状态。 <template> <div>…

vue VUE实现日历组件功能

vue VUE实现日历组件功能

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

vue radio组件实现

vue radio组件实现

Vue Radio 组件实现 在 Vue 中实现 Radio 组件可以通过原生 HTML 的 <input type="radio"> 结合 Vue 的响应式特性来完成。以下是几种常见的实…

vue实现tooltips组件

vue实现tooltips组件

实现基础 Tooltip 组件 创建一个名为 Tooltip.vue 的单文件组件,包含模板、样式和逻辑: <template> <div class="tooltip-con…

vue实现水印组件

vue实现水印组件

Vue 水印组件的实现方法 使用Canvas绘制水印 在Vue组件中通过Canvas动态生成水印图案,将其作为背景添加到目标元素上。 <template> <div ref=…

vue抽屉组件实现

vue抽屉组件实现

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