This time I will bring you Vue to implement the countdown button. What are the precautions for Vue to implement the countdown button? The following is a practical case, let's take a look.
In project development, we often encounter buttons that send 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) } }
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.
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 = '重新发送验证码' this.totalTime = 60 } },1000) },
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: '发送验证码', totalTime: 10, canClick: true //添加canClick } } ... countDown () { if (!this.canClick) return //改动的是这两行代码 this.canClick = false this.content = this.totalTime + 's后重新发送' let clock = window.setInterval(() => { this.totalTime-- this.content = this.totalTime + 's后重新发送' if (this.totalTime < 0) { window.clearInterval(clock) this.content = '重新发送验证码' this.totalTime = 10 this.canClick = true //这里重新开启 } },1000) }
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; // 鼠标变化 }
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:
Detailed explanation of the use of .sync modifier in vue
jQuery$. and $(). use Detailed explanation
The above is the detailed content of Vue implements countdown button. For more information, please follow other related articles on the PHP Chinese website!