search
HomeWeb Front-endJS TutorialExample of VUE component: How does VUE component implement countdown?

This article brings you examples of VUE components: How does the VUE component implement countdown? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Rendering:

2. CSS code

.box{
    width: 300px;
    height: 100px;
    line-height: 100px;
    margin: 100px auto;
    background-color: #eee;
    text-align: center;
    font-size: 30px;
    color: #666;
}.count-number{
    color: red;
    font-size: 30px;
    font-weight: bold;
}

3 , HTML code

<p class="wrap">
    <countdown seconds="15" index="1"></countdown>
    <countdown seconds="30" index="2"></countdown></p>

4. JavaScript code

// 倒计时组件
Vue.component(&#39;countdown&#39;, {
    props: [&#39;seconds&#39;, &#39;index&#39;],
    data: () => {
         return {
            timerCount: 0
         }
     },
     mounted() {
         this.timing();
     },
     methods: {
         timing() {
             let startTime = localStorage.getItem(`startTime${this.index}`);
             let nowTime = new Date().getTime();
             if(startTime) {
                  let surplus = this.seconds - parseInt((nowTime-startTime) / 1000, 10);
                  this.timerCount = surplus <= 0 ? 0 : surplus;
             } else {
                  this.timerCount = this.seconds;
                  localStorage.setItem(`startTime${this.index}`, nowTime);
             }

             let timer = setInterval(() => {
                  if(this.timerCount > 0 && this.timerCount <= this.seconds) {
                      this.timerCount--;
                 } else {
                      localStorage.removeItem(`startTime${this.index}`);
                      clearInterval(timer);
                  }
             }, 1000);
         }
    },
    template: `<div class="box">倒计时 <span class="count-number">{{timerCount < 10 ? &#39;0&#39; + timerCount : timerCount}}</span> 秒</div>`
});

// Vue实例
new Vue({
    el: &#39;.wrap&#39;
});

Related recommendations:

Prototype mechanism in JS: Implement inheritance and extension of constructors and their instances

Communication between Vue child components and parent components (with code)

The above is the detailed content of Example of VUE component: How does VUE component implement countdown?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Vue如何实现组件的复用和扩展?Vue如何实现组件的复用和扩展?Jun 27, 2023 am 10:22 AM

随着前端技术的不断发展,Vue已经成为了前端开发中的热门框架之一。在Vue中,组件是其中的核心概念之一,它可以将页面分解为更小,更易管理的部分,从而提高开发效率和代码复用性。本文将重点介绍Vue如何实现组件的复用和扩展。一、Vue组件复用mixinsmixins是Vue中的一种共享组件选项的方式。Mixins允许将多个组件的组件选项合并成一个对象,从而最大

Vue组件通信:使用$destroy进行组件销毁通信Vue组件通信:使用$destroy进行组件销毁通信Jul 09, 2023 pm 07:52 PM

Vue组件通信:使用$destroy进行组件销毁通信在Vue开发中,组件通信是非常重要的一个方面。Vue提供了多种方式来实现组件通信,比如props和emit、vuex等。本文将介绍另一种组件通信方式:使用$destroy进行组件销毁通信。在Vue中,每个组件都有一个生命周期,其中包含了一系列的生命周期钩子函数。组件的销毁也是其中之一,Vue提供了一个$de

Vue实战:日期选择器组件开发Vue实战:日期选择器组件开发Nov 24, 2023 am 09:03 AM

Vue实战:日期选择器组件开发引言:日期选择器是在日常开发中经常用到的一个组件,它可以方便地选择日期,并提供各种配置选项。本文将介绍如何使用Vue框架来开发一个简单的日期选择器组件,并提供具体的代码示例。一、需求分析在开始开发之前,我们需要进行需求分析,明确组件的功能和特性。根据常见的日期选择器组件功能,我们需要实现以下几个功能点:基础功能:能够选择日期,并

Vue组件通信:使用watch和computed进行数据监听Vue组件通信:使用watch和computed进行数据监听Jul 10, 2023 am 09:21 AM

Vue组件通信:使用watch和computed进行数据监听Vue.js是一款流行的JavaScript框架,它的核心思想是组件化。在一个Vue应用中,不同的组件之间需要进行数据的传递和通信。在这篇文章中,我们将介绍如何使用Vue的watch和computed来进行数据的监听和响应。watch在Vue中,watch是一个选项,它可以用来监听一个或多个属性的变

Vue开发注意事项:如何处理复杂数据结构和算法Vue开发注意事项:如何处理复杂数据结构和算法Nov 22, 2023 am 08:08 AM

在Vue开发中,我们经常会遇到处理复杂数据结构和算法的情况。这些问题可能涉及大量的数据操作、数据同步、性能优化等方面。本文将介绍一些处理复杂数据结构和算法的注意事项和技巧,帮助开发者更好地应对这些挑战。一、数据结构的选择在处理复杂数据结构和算法时,选择合适的数据结构非常重要。Vue提供了丰富的数据结构和方法,开发者可以根据实际需求选择合适的数据结构。常用的数

Vue项目中如何使用第三方库Vue项目中如何使用第三方库Oct 15, 2023 pm 04:10 PM

Vue是一款流行的JavaScript框架,它提供了丰富的工具和功能,可以帮助我们构建现代化的Web应用程序。尽管Vue本身已经提供了许多实用的功能,但有时候我们可能需要使用第三方库来扩展Vue的能力。本文将介绍在Vue项目中如何使用第三方库,并提供具体的代码示例。1.引入第三方库在Vue项目中使用第三方库的第一步是引入它们。我们可以通过以下几种方式来引入

Vue组件开发:标签页组件实现方法Vue组件开发:标签页组件实现方法Nov 24, 2023 am 08:41 AM

Vue组件开发:标签页组件实现方法在现代Web应用程序中,标签页(Tab)是一个广泛使用的UI组件。标签页组件可以在单个页面上显示多个相关内容,并通过单击标签来切换它们。在本文中,我们将介绍如何使用Vue.js实现一个简单的标签页组件,并提供详细的代码示例。Vue标签页组件的结构标签页组件通常由两个部分组成:标签(Tab)和面板(Panel)。标签用于标识面

Vue组件通信:使用vuex进行状态管理通信Vue组件通信:使用vuex进行状态管理通信Jul 09, 2023 am 10:16 AM

Vue组件通信:使用vuex进行状态管理通信引言:在Vue开发中,组件之间的通信是一个关键的问题。Vue提供了多种方式来实现组件通信,其中使用vuex进行状态管理是一种常见的方式。本文将介绍如何使用vuex进行组件通信,并提供代码示例帮助读者更好地理解。一、什么是vuexVuex是Vue.js的官方状态管理库,它可以实现全局状态的管理和组件之间的通信。Vue

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.