Use momentJs to make a countdown component (example code)

Use momentJs to make a countdown component (example code)

Today I'd like to introduce a countdown made by vue and moment. The specific content is as follows:

Display style:

<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: { }, //Last received time 2021-12-17 16:29:20
  },
  data() {
    //Store data here 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)
  },
  //Method collection 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)); //Remaining days, you can make up for it if necessary let h = Math.floor((t - 24 * 3600 * d) / 3600) + d*24; //No days needed, convert days into hours let _h = Math.floor((t - 24 * 3600 * d) / 3600) //Keep the days and get the hours 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') //Callback when countdown ends}
 
  },
  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>

This is the end of this article about using momentJs to make a countdown component. For more related momentJs countdown component content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue implements CLI 3.0 + momentjs + lodash packaging optimization

<<:  Detailed explanation of the frame and rules attributes of the table in HTML

>>:  Advantages and disadvantages of Table layout and why it is not recommended

Recommend

How to view Linux ssh service information and running status

There are many articles about ssh server configur...

Detailed explanation of the use of Linux lseek function

Note: If there are any errors in the article, ple...

Summary of common problems and solutions in Vue (recommended)

There are some issues that are not limited to Vue...

How to remove the blue box that appears when the image is used as a hyperlink

I recently used Dreamweaver to make a product pres...

Tutorial on installing AutoFs mount service under Linux

Whether it is Samba service or NFS service, the m...

JavaScript basics for loop and array

Table of contents Loop - for Basic use of for loo...

Q&A: Differences between XML and HTML

Q: I don’t know what is the difference between xml...

A tutorial on how to install, use, and automatically compile TypeScript

1. Introduction to TypeScript The previous articl...

SQL Server database error 5123 solution

Because I have a database tutorial based on SQL S...

JavaScript to achieve floor effect

This article shares the specific code of JavaScri...

Detailed tutorial on installing MySQL 8.0.19 in zip version on win10

Table of contents 1. After downloading, unzip it ...

Introduction to fuzzy query method using instr in mysql

Using the internal function instr in MySQL can re...

Methods and steps for deploying multiple war packages in Tomcat

1 Background JDK1.8-u181 and Tomcat8.5.53 were in...