当前位置:首页 > 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中实现分页功能,通常需要父子组件之间的数据传递。以下是几种常见的传值方法: 使用props和$emit 父组件通过props向子组件传递数据,子组件通过$emit…

vue组件实现

vue组件实现

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

vue实现组件

vue实现组件

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

vue实现组件跟随

vue实现组件跟随

实现组件跟随的常见方法 使用CSS定位 通过CSS的position: fixed或position: absolute属性实现组件跟随。需要结合JavaScript计算目标元素的位置。 <…

vue实现拖拽实现组件嵌套

vue实现拖拽实现组件嵌套

Vue 拖拽实现组件嵌套 使用 Vue 实现拖拽组件嵌套功能可以通过原生 HTML5 拖拽 API 或第三方库如 vuedraggable 实现。以下是两种方法的详细说明: 使用 HTML5 拖拽…

vue轮播组件实现

vue轮播组件实现

Vue 轮播组件实现方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template&g…