利用momentJs做一个倒计时组件(实例代码)

2022-04-15 0 1,135

今天抽空给大家介绍下vue和moment做的一个倒计时,具体内容如下所示:

展示样式:

利用momentJs做一个倒计时组件(实例代码)

<template>
    <div class="table-right flex-a-center">
        <div class="time-text">
            <span class="timeTextSpan" v-for="item,index of h" >{{item}}</span>
            <span class="timeTextSpan1" >: </span>
            <span class="timeTextSpan" v-for="item,index of m" >{{item}}</span>
            <span class="timeTextSpan1" >: </span>
            <span class="timeTextSpan" v-for="item,index of s" >{{item}}</span>
        </div>
    </div>
</template>
<script>
import moment from 'moment'
export default {
  props: {
    endTime: { }, //接收得最后时间 2021-12-17 16:29:20
  },
  data() {
    //这里存放数据
    return {
      h:'00',
      m:'00',
      s:'00',
      timer:null
    };
  },
  watch: {
    endTime: {
      handler(e) {
        if (e) {
          let self = this
          clearInterval(this.timer)
          this.timer = setInterval(function(){self.init()},1000)
        }
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {
    let self = this
    self.init()
    clearInterval(this.timer)
    this.timer = setInterval(function(){self.init()},1000)
  },
  //方法集合
  methods: {
    init(){
        let time =moment(this.endTime).diff(moment())
        if(time <= 0){
          clearInterval(this.timer)
          this.onOver()
          return
        }
        let t = time / 1000;
        let d = Math.floor(t / (24 * 3600));  //剩余天数,如果需要可以自行补上
        let h = Math.floor((t - 24 * 3600 * d) / 3600) + d*24;  //不需要天数,把天数转换成小时
        let _h = Math.floor((t - 24 * 3600 * d) / 3600)  //保留天数后得小时
        let m = Math.floor((t - 24 * 3600 * d - _h * 3600) / 60);
        let s = Math.floor((t - 24 * 3600 * d - _h * 3600 - m * 60));
       
        this.h = String(h).length == 1? '0'+String(h):String(h)
        this.m = String(m).length == 1? '0'+String(m):String(m)
        this.s = String(s).length == 1? '0'+String(s):String(s)
    },
    onOver() {
      this.$emit('over') //倒计时结束得回调
    }
 
  },
  beforeDestroy(){
    this.timer = null
    clearInterval(this.timer)
  }
}
</script>
<style lang='less' scoped>
@import url("@/assets/css/supplier.less");
 

  .table-right {
    font-size: 12px;
    color: #757e8a;
    .timeTextSpan{
      display: inline-block;
      width: 14px;
      height: 22px;
      text-align: center;
      background: #F1F0F0;
      border-radius: 2px;
      margin-right: 2px;
      font-size: 16px;
      color: #ff8a2b;
      font-weight: bold;
    }
    .timeTextSpan1{
      display: inline-block;
      width: 14px;
      text-align: center;
      vertical-align: bottom;
      color:#202D40;
      font-size: 16px;
      font-weight: bold;
    }
   
    .time-text {
      margin-left: 10px;
    }
  }
</style>

到此这篇关于利用momentJs做一个倒计时组件的文章就介绍到这了,更多相关momentJs倒计时组件内容请搜索NICE源码以前的文章或继续浏览下面的相关文章希望大家以后多多支持NICE源码!

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 JavaScript 利用momentJs做一个倒计时组件(实例代码) https://www.niceym.com/21619.html