Vue implements the countdown component for second kills

Vue implements the countdown component for second kills

This article shares the specific code of Vue to implement the second kill countdown component for your reference. The specific content is as follows

Below is the countdown component for the second kill using Vue

Development ideas

1. Request the server to obtain the server time at this moment (based on the server time)
2. Get the user's current computer time and compare it with the server time to get the time difference. The final time is the current computer time minus the time difference (defined as the current server time)
3. Set the start time of the flash sale
4. Compare the flash sale time with the current server time to get the time difference (X seconds in total)
5. Calculate the number of days, hours, minutes, and seconds until the flash sale starts based on X seconds.

Code Implementation

The following code only shows steps 4 and 5.

Component CountDown.vue

<template>
  <div>
      <input type="datetime-local" :min="currentTime" placeholder="Please enter the start time of the flash sale" v-model="startTime">
      <button @click="submit">Start timing</button>
  </div>
  <div>
      <h1>{{ countDownTime }}</h1>
  </div>
</template>

<script>
let timer = null;
let tipTextPrefix = 'Time left until the flash sale starts: ';
import moment from "moment";
export default {
    name: 'CountDown',
    data() { return {
        currentTime: moment().format('YYYY-MM-DDTHH:mm'), // Please use the server time calculated in steps 1-3 startTime: moment().format('YYYY-MM-DDTHH:mm'),
        countDownTime: tipTextPrefix + '0 days 0 hours 0 minutes 0 seconds'
    }},
    methods: {
        submit: function() {
            const _this = this;
            clearInterval(timer);
            timer = setInterval(() => {
                _this.countDownTime = _this.countDown();
            }, 1000);
        },
        countDown: function() {
            console.log(this.startTime);
            const seconds = moment(this.startTime).diff(new Date, 'seconds');
            if (seconds <= 0) {
                clearInterval(timer);
                return 'Second sale has started';
            }
            const second = seconds%60;
            const minutes = (seconds-second) / 60;
            const minute = minutes%60;
            const hours = (minutes-minute) / 60;
            const hour = hours%24;
            const day = (hours-hour) / 24;
            const res = tipTextPrefix + day + 'day' + hour + 'hour' + minute + 'minute' + second + 'second';
            return res;
        }
    },
}
</script>

<style>

</style>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Solve the problem that vue project cannot carry cookies when started locally
  • Solution to the error "cannot GET /service" when starting a vue project
  • Vue modify project startup port number method
  • Detailed explanation of how to start running the dist file generated after webpack packages the vue project
  • Detailed explanation of VSCode configuration to start Vue project
  • Simple configuration method for hot start debugging of webpack+vue+express(hot)
  • Analysis of the reasons why vue-cli cannot access the local service LAN
  • Detailed explanation of vue express starting data service
  • Vue implements the product tab of the product details page function
  • Detailed explanation of the implementation principle of Vue2.0/3.0 two-way data binding
  • How to start a Vue.js project

<<:  Solution to the problem of flash back after entering the password in MySQL database

>>:  Detailed explanation of Nginx's connection limit configuration for IP addresses in a network segment

Recommend

Summary of MySQL slow log related knowledge

Table of contents 1. Introduction to Slow Log 2. ...

Summary of some small issues about MySQL auto-increment ID

The following questions are all based on the Inno...

Examples of using Docker and Docker-Compose

Docker is an open source container engine that he...

Practice of dynamically creating dialog according to file name in vue+el-element

Table of contents background accomplish 1. Encaps...

How to implement rounded corners with CSS3 using JS

I found an example when I was looking for a way t...

Summary of MySQL usage specifications

1. InnoDB storage engine must be used It has bett...

MySQL 5.7.17 installation and configuration method graphic tutorial (windows)

1. Download the software 1. Go to the MySQL offic...

js to achieve star flash effects

This article example shares the specific code of ...

Solve the 1251 error when establishing a connection between mysql and navicat

I reinstalled the computer and installed the late...

Some suggestions for HTML beginners and novices, experts can ignore them

Feelings: I am a backend developer. Sometimes when...

Do you know how to optimize loading web fonts?

Just as the title! The commonly used font-family l...

Summary of learning Docker commands in one article

Table of contents Introduction Mirror repository ...

Use of Linux gzip command

1. Command Introduction The gzip (GNU zip) comman...

How to automatically deploy Linux system using PXE

Table of contents Background Configuring DHCP Edi...

Implementation ideas for docker registry image synchronization

Intro Previously, our docker images were stored i...