vue实现倒计时效果

效果地址: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)
    }
  }
})
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。