当前位置:首页 > VUE

vue实现倒计时

2026-01-11 20:20:04VUE

Vue 实现倒计时的方法

使用 setInterval 实现基础倒计时

通过 Vue 的 data 定义倒计时时间,使用 setInterval 进行倒计时操作,并在组件销毁时清除定时器。

<template>
  <div>{{ countdown }}</div>
</template>

<script>
export default {
  data() {
    return {
      countdown: 60, // 初始倒计时时间(秒)
      timer: null
    };
  },
  mounted() {
    this.startCountdown();
  },
  methods: {
    startCountdown() {
      this.timer = setInterval(() => {
        if (this.countdown > 0) {
          this.countdown--;
        } else {
          clearInterval(this.timer);
        }
      }, 1000);
    }
  },
  beforeDestroy() {
    clearInterval(this.timer);
  }
};
</script>

使用 computed 格式化显示时间

通过 computed 属性将秒数格式化为 分钟:秒 的形式,提升可读性。

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

<script>
export default {
  data() {
    return {
      countdown: 120 // 初始倒计时时间(秒)
    };
  },
  computed: {
    formattedTime() {
      const minutes = Math.floor(this.countdown / 60);
      const seconds = this.countdown % 60;
      return `${minutes}:${seconds < 10 ? '0' + seconds : seconds}`;
    }
  },
  mounted() {
    setInterval(() => {
      if (this.countdown > 0) {
        this.countdown--;
      }
    }, 1000);
  }
};
</script>

使用 Vue 3 Composition API 实现倒计时

在 Vue 3 中,可以通过 refonMounted 等 Composition API 实现倒计时功能。

vue实现倒计时

<template>
  <div>{{ countdown }}</div>
</template>

<script>
import { ref, onMounted, onBeforeUnmount } from 'vue';

export default {
  setup() {
    const countdown = ref(60);
    let timer = null;

    const startCountdown = () => {
      timer = setInterval(() => {
        if (countdown.value > 0) {
          countdown.value--;
        } else {
          clearInterval(timer);
        }
      }, 1000);
    };

    onMounted(startCountdown);
    onBeforeUnmount(() => clearInterval(timer));

    return { countdown };
  }
};
</script>

结合 localStorage 实现持久化倒计时

通过 localStorage 存储倒计时剩余时间,确保页面刷新后倒计时继续。

<template>
  <div>{{ countdown }}</div>
</template>

<script>
export default {
  data() {
    return {
      countdown: localStorage.getItem('countdown') || 60
    };
  },
  mounted() {
    this.timer = setInterval(() => {
      if (this.countdown > 0) {
        this.countdown--;
        localStorage.setItem('countdown', this.countdown);
      } else {
        clearInterval(this.timer);
        localStorage.removeItem('countdown');
      }
    }, 1000);
  },
  beforeDestroy() {
    clearInterval(this.timer);
  }
};
</script>

使用第三方库 vue-countdown

如果需要更复杂的倒计时功能,可以使用第三方库如 vue-countdown

vue实现倒计时

安装依赖:

npm install vue-countdown

使用示例:

<template>
  <vue-countdown :time="60 * 1000" v-slot="{ minutes, seconds }">
    {{ minutes }}:{{ seconds }}
  </vue-countdown>
</template>

<script>
import VueCountdown from 'vue-countdown';

export default {
  components: { VueCountdown }
};
</script>

以上方法涵盖了从基础实现到高级功能的多种倒计时方案,可以根据实际需求选择适合的方式。

标签: 倒计时vue
分享给朋友:

相关文章

vue实现遮罩

vue实现遮罩

Vue 实现遮罩层的方法 在 Vue 中实现遮罩层可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-show 或 v-if 控制显示 通过 Vue 的指令动态控制遮罩层的显示与隐藏。…

vue slot实现

vue slot实现

vue slot 的实现方法 在 Vue 中,slot 是一种内容分发机制,允许父组件向子组件传递模板内容。以下是几种常见的 slot 实现方式: 默认 slot 默认 slot 是最基础的 slo…

vue 实现href

vue 实现href

Vue 实现 href 的方法 在 Vue 中实现 href 功能可以通过多种方式,具体取决于需求场景。以下是几种常见的方法: 使用 <a> 标签 直接使用 HTML 的 <a&g…

vue实现动画

vue实现动画

Vue 实现动画的方法 Vue 提供了多种方式来实现动画效果,包括内置的过渡系统、第三方动画库以及直接操作 CSS 或 JavaScript 动画。 使用 Vue 内置过渡系统 Vue 的 <…

vue实现slidetoggle

vue实现slidetoggle

Vue 实现 SlideToggle 效果 SlideToggle 是一种常见的交互效果,元素以滑动方式展开或收起。以下是几种实现方法: 使用 CSS Transition 和 v-show…

vue实现github

vue实现github

Vue 实现 GitHub 相关功能 创建 GitHub API 请求 在 Vue 项目中,可以使用 axios 或 fetch 与 GitHub API 进行交互。需要在 GitHub 上创建个人访…