Home Web Front-end JS Tutorial How to use Vue to implement a countdown button

How to use Vue to implement a countdown button

Jun 02, 2018 pm 02:51 PM
Countdown accomplish button

This time I will show you how to use Vue to implement the countdown button, and what are the precautions for using Vue to implement the countdown button. The following is a practical case, let's take a look.

In project development, we often encounter a button that sends a verification code and has a 60-second countdown after clicking it. It is very common but also very simple, but there are certain places that you need to pay attention to when writing this button. Next, I will write it down today. If you have any questions, please correct me!

The completed effect is as follows:

In order to show the effect faster, I set the time to 5 seconds. After clicking the button, a countdown will appear. At the same time, the button will become unclickable, its style will also change, and the appearance of the mouse hovering will also change.

Next we use code to implement it:

<button class="button" @click="countDown">
 {{content}}
</button>
...
data () {
  return {
   content: '发送验证码',  // 按钮里显示的内容
   totalTime: 60      //记录具体倒计时时间
  }
},
methods: {
  countDown() {
    let clock = window.setInterval(() => {
      this.total--
      this.content = this.total + 's后重新发送'
    },1000)
  }
}
Copy after login
Add two pieces of data to the data, one to record the time, and one to hold the specific content of the countdown button. In the countDown function, we use the setInterval timer to decrement the totalTime by 1 every second and change the content displayed in the button. The arrow function is used in window.setInterval because it will automatically bind the external this, so there is no need to save this first.

The effect is as shown below:

But there are still some problems with this button:

After clicking the button, after 1 second, it will go directly from The countdown starts at 59 seconds, and the 60 in the middle is gone.

You can still click during the countdown.
The countdown has not been cleared yet.

Next, you need to continue to improve the countDown function to solve these problems.

countDown () {
 this.content = this.totalTime + 's后重新发送' //这里解决60秒不见了的问题
 let clock = window.setInterval(() => {
  this.totalTime--
  this.content = this.totalTime + 's后重新发送'
  if (this.totalTime < 0) {     //当倒计时小于0时清除定时器
    window.clearInterval(clock)
    this.content = &#39;重新发送验证码&#39;
    this.totalTime = 60
    }
 },1000)
},
Copy after login

The above code solves the problem of 60 missing. At the same time, when totalTime is less than 0, it clears the synchronizer, resets the content in the button, and resets totalTime to 60 for next time use.

The effect of counting down for 10 seconds:

Found a bug. After clicking multiple times, the speed of rewinding becomes faster. This is because each click will Start a setInterval, these setIntervals will reduce totalTime. The solution is also very simple: simply throttle it, that is, make the code of the countDonw function non-executable after the first click of the button, and wait until the countdown is over before it can be executed again.

data () {
  return {
   content: &#39;发送验证码&#39;,
   totalTime: 10,
   canClick: true //添加canClick
  }
}
...
countDown () {
 if (!this.canClick) return  //改动的是这两行代码
 this.canClick = false
 this.content = this.totalTime + &#39;s后重新发送&#39;
 let clock = window.setInterval(() => {
  this.totalTime--
  this.content = this.totalTime + 's后重新发送'
  if (this.totalTime < 0) {
   window.clearInterval(clock)
   this.content = &#39;重新发送验证码&#39;
   this.totalTime = 10
   this.canClick = true  //这里重新开启
  }
 },1000)
}
Copy after login

Add canClick in data. The default is true. If canClick is true, the code in countDown can be executed. If it is false, it will not work. Set canClick to false every time it is executed, and only change it to true when the countdown ends. This way the problem just now disappears.

It’s almost done here, but you can also adjust the style:

<button class="button" :class="{disabled: !this.canClick}" @click="countDown">
...
.disabled{
 background-color: #ddd;
 border-color: #ddd;
 color:#57a3f3;
 cursor: not-allowed; // 鼠标变化
}
Copy after login
Effect:

This countdown button is very simple, but it was still very messy when I first wrote it, and I didn’t know the concept of function throttling at that time.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use Vue to write a two-way data binding

##How to use Vue to implement proxy proxy

The above is the detailed content of How to use Vue to implement a countdown button. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

Why won't my laptop start up after pressing the power button? Why won't my laptop start up after pressing the power button? Mar 10, 2024 am 09:31 AM

There could be several reasons why your Windows laptop won't boot. Memory failure, dead battery, faulty power button, or hardware issues are all common causes. Here are some solutions to help you resolve this issue. Laptop won't turn on after pressing the power button If your Windows laptop still won't turn on after pressing the power button, here are some steps you can take to resolve the issue: Is your laptop fully charged? Perform a hard reset to clean your laptop Reseat the memory Transparent CMOS type battery Take your laptop for repair. 1] Is your laptop fully charged? The first thing to do is to check if your laptop is fully charged. Laptop won't start if battery is drained

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

How to solve the problem of unresponsive buttons in IE browser How to solve the problem of unresponsive buttons in IE browser Jan 30, 2024 am 10:48 AM

What should I do if there is no response when clicking the web button in IE browser? If there is no response when we click the web button, we can set it in the compatibility view! When some friends are using the IE browser, they find that the browser will not respond when clicking the button on the web page. In this way, we cannot use the functions of the web page. How can we set it up? The editor has compiled the IE browser below. How to solve the problem of the browser not responding when clicking the web button? If not, follow me and read on! IE browser does not respond when clicking the web button. Solution: 1. Open IE browser, click the [Tools] button on the operation bar, and click [Compatibility View] settings, as shown in the figure. 2. In the [Compatibility View] setting page, click the [Add] button on the right and fill in the website.

What does iPhone 16 look like? What changes are there in iPhone 16? What does iPhone 16 look like? What changes are there in iPhone 16? Apr 07, 2024 pm 05:10 PM

After the release of the iPhone 15 series, there have been constant revelations about the appearance and configuration of Apple’s new iPhone 16. What does iPhone 16 look like? Is there any improvement in iPhone 16? Recently, an overseas blogger showed off the design of the iPhone 16 series. The overall design is basically the same as the iPhone 15 series. As you can see from the picture, the entire iPhone 16 series is equipped with a new "shoot" button as standard, allowing users to take photos more conveniently. In addition, other design details are still unknown. The message shows that this new button will be used to shoot videos and is located below the power button. Previous news has mentioned that it may be a capacitive solid-state button, but recent reports indicate that it should still be a

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

Master how Golang enables game development possibilities Master how Golang enables game development possibilities Mar 16, 2024 pm 12:57 PM

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide PHP Game Requirements Implementation Guide Mar 11, 2024 am 08:45 AM

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

See all articles