vue实现倒计时效果

本文介绍了一种使用Vue.js实现的按钮点击倒计时效果,通过禁用按钮并显示剩余时间来防止重复点击,时间结束后按钮恢复正常状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果地址:https://codepen.io/jmwei/pen/XYBpaw

说明:使用vue实现按钮点击倒计时效果

代码实现:

<div id="app">
  <button class="button" :class="{disabled: !this.canClick}" @click="countDown">
    {{content}}
  </button>
</div>

样式优化

.button{
  color: #fff;
  background: #409eff;
  cursor: pointer;
  border: 1px solid #dcdfe6;
  border-color: #dcdfe6;
  text-align: center;
  font-weight: 500;
  padding: 7px 20px;
  font-size: 14px;
  border-radius: 4px;
  &:focus {
    outline: none;
  }
}
.disabled {
  background-color: #ddd;
  border-color: #ddd;
  color:#57a3f3;
  cursor: not-allowed;  
}

js操作

var app = new Vue({
  el: '#app',
  data: {
    content: '倒计时',
    totalTime: 5,
    canClick: true,
  },
  methods: {
    countDown() {
      if (!this.canClick) {
        return
      }
      this.canClick = false;
      this.content = this.totalTime + 's后重新发送';
      let clock = setInterval(() => {
        this.totalTime --;
        this.content = this.totalTime + 's后重新发送';
        if (this.totalTime < 0) {
           window.clearInterval(clock);
          this.content = '倒计时';
          this.totalTime = 5;
          this.canClick = true;
        }
      }, 1000)
    }
  }
})

 

转载于:https://www.cnblogs.com/jmwei/p/10942159.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值