Vue分享一个基于element的自己写的倒计时组件

倒计时,计时结束弹出消息框

不多废话,直接上代码 

<template>
  <div class="timeCount-container">
    <el-card class="box-card" id="asidecrad">
      <div slot="header" class="clearfix">
        <span>剩余时间</span>
      </div>
      <div>{{ this.hour }} 时 {{ this.minute }} 分 {{ this.second }} 秒</div>
    </el-card>
  </div>
</template>
<script>
export default {
  name: "timeCount",
  data() {
    return {
      endTime: 0,
      hour: 0,
      minute: 0,
      second: 0,
      interval: null,
    };
  },
  //传值
  props: {
    //倒计时时间
    longTime: {
      type: Number,
      default: 0,
    },
      //弹出框标题
      messageTitle:{
          type:String,
          default: ''
      },
      //弹出框内容
       messageText:{
          type:String,
          default: ''
      },
  },
  mounted() {
    //结束时间
    this.endTime = Date.parse(new Date()) + this.longTime * 1000;
    //开始倒计时
    this.countdown();
  },
  beforeDestroy() {
    //提前结束则销毁倒计时
    clearTimeout(this.interval);
  },
  methods: {
    countdown() {
      //计算运行时间
      const end = this.endTime;
      const now = Date.parse(new Date());
      const msec = end - now;
      //计算剩余时间
      this.second = (msec / 1000) % 60;
      this.minute = parseInt((msec / 1000 / 60) % 60);
      this.hour = parseInt(msec / 1000 / 60 / 60);

      console.log(this.hour, this.minute, this.second);

      console.log(msec);
      const that = this;
      //结束则弹框
      if (msec === 0) {
        this.$alert(this.messageTitle, this.messageText, {
          confirmButtonText: "确定",
          callback: () => {
            this.$router.push("/home/myexam");
          },
        });
      } else {
        //进入下一秒倒计时
        this.interval = setTimeout(that.countdown, 1000);
      }
    },
  },
};
</script>

 

<style lang='less' scoped>
.timeCount-container {
  box-shadow: 0 0 8px rgba(0, 0, 0, 0.2);
  text-align: center;
}
</style>

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来给您一个基于 VueElementUI 的亚运会倒计时。 首先 Vue 项目中安装 ElementUI: ``` npm install element-ui --save ``` 然后在 main.js 中引入 ElementUI: ```javascript import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 接着,我们在组件中使用 ElementUI 的 CountDown 组件来实现倒计时功能: ```html <template> <div class="countdown"> <el-count-down :time="countdownTime" :auto-start="false" @finish="handleFinish"> <template slot-scope="{ days, hours, minutes, seconds }"> <div class="countdown-item"> <span class="countdown-value">{{ days }}</span> <span class="countdown-label">天</span> </div> <div class="countdown-item"> <span class="countdown-value">{{ hours }}</span> <span class="countdown-label">时</span> </div> <div class="countdown-item"> <span class="countdown-value">{{ minutes }}</span> <span class="countdown-label">分</span> </div> <div class="countdown-item"> <span class="countdown-value">{{ seconds }}</span> <span class="countdown-label">秒</span> </div> </template> </el-count-down> </div> </template> <script> export default { data() { return { countdownTime: new Date('2022-09-10T00:00:00Z') // 2022年9月10日亚运会开幕时间 } }, methods: { handleFinish() { // 倒计时结束的回调函数 console.log('倒计时结束') } } } </script> <style scoped> .countdown { display: flex; } .countdown-item { display: flex; flex-direction: column; align-items: center; margin-right: 10px; } .countdown-value { font-size: 24px; font-weight: bold; } .countdown-label { font-size: 12px; color: #aaa; } </style> ``` 以上代码实现了一个简单的亚运会倒计时,其中我们使用了 ElementUI 的 CountDown 组件,并设置了倒计时结束的回调函数 handleFinish。在模板中使用了 slot-scope 来获取倒计时的天数、小时数、分钟数和秒数,并将它们渲染到页面上。 希望对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值