当前位置:首页 > 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.js 实现手册的整理内容,涵盖核心概念、实践方法和常见场景的解决方案: 核心概念与基础用法 Vue.js 是一个渐进式 JavaScript 框架,核心特点包括数据驱动视图和组…

vue实现文档分享

vue实现文档分享

Vue 实现文档分享功能 文档分享功能通常涉及文件上传、存储、生成分享链接以及权限控制等模块。以下是基于 Vue 的实现方案: 文件上传组件 使用 element-ui 或 ant-design-…

vue实现多人视频

vue实现多人视频

实现多人视频通话的Vue方案 使用WebRTC技术结合Vue框架可以构建多人视频通话应用。以下是核心实现方法: 技术栈选择 Vue 2/3作为前端框架 WebRTC用于实时通信 Socket.io…

vue实现放大效果

vue实现放大效果

使用 CSS transform 实现放大效果 在 Vue 中实现放大效果可以通过 CSS 的 transform: scale() 属性。这种方法简单高效,适合大多数交互场景。 定义一个 CSS…

vue前端实现登录

vue前端实现登录

实现登录功能的基本流程 使用Vue实现登录功能需要结合前端框架和后端API交互。以下是一个典型的实现方案: 安装必要依赖 确保项目已安装axios用于HTTP请求: npm install axi…

vue前端实现搜索

vue前端实现搜索

实现搜索功能的基本方法 在Vue中实现搜索功能通常涉及以下几个关键步骤,结合数据绑定、计算属性和方法调用来动态过滤和显示结果。 数据绑定与输入处理 使用v-model双向绑定搜索输入框的值,监听用户…