当前位置:首页 > 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 接收外部传入的初始时间,增强组件复用性。

<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)。

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 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。组件的实现包括定义、注册、数据传递和生命周期管理等。 定义组件 组件可以通过单文件组件(.…

vue实现组件

vue实现组件

Vue 组件实现基础 Vue 组件是可复用的 Vue 实例,用于封装 UI 和逻辑。通过 .vue 文件或直接注册组件实现。 单文件组件 (SFC) 示例 <template>…

vue实现广告组件

vue实现广告组件

Vue 实现广告组件的核心方法 数据驱动的广告内容渲染 通过 props 接收广告数据(如图片URL、跳转链接等),使用 v-bind 动态绑定属性。典型结构包含 <a> 标签嵌套 <…

Vue组件实现方法

Vue组件实现方法

Vue组件的基本实现方法 Vue组件是Vue.js的核心概念之一,用于构建可复用的UI模块。以下是Vue组件的几种常见实现方式: 单文件组件(SFC) 使用.vue文件格式,将模板、脚本和样式封装在…

vue实现组件封装

vue实现组件封装

Vue 组件封装方法 封装组件的基本步骤 创建独立的 .vue 文件,包含 template、script 和 style 三个部分。通过 props 接收父组件传递的数据,使用 $emit 触发…

vue radio组件实现

vue radio组件实现

Vue Radio 组件实现 在 Vue 中实现 Radio 组件可以通过原生 HTML 的 <input type="radio"> 结合 Vue 的响应式特性,或者使用第三方 UI 库…