意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

用vue怎样写倒计时按钮,代码是什么

来源:恒创科技 编辑:恒创科技编辑部
2024-01-22 19:06:59
这篇文章给大家介绍了“用vue怎样写倒计时按钮,代码是什么”的相关知识,讲解详细,步骤过程清晰,有一定的借鉴学习价值,因此分享给大家做个参考,感兴趣的朋友接下来一起跟随小编看看吧。

 


用vue怎样写倒计时按钮,代码是什么

实现效果:

1.点击开始按钮启动计时

2.点击重置按钮计时恢复到00:00:00

3.点击暂停按钮暂停计时

Vue代码:

<template>
 <div>
  <div class="timeContainer">{{ time }}</div>
  <a-button style="margin-right: 20px" type="primary" @click="start"
   >开始</a-button
  >
  <a-button style="margin-right: 20px" type="primary" @click="reset"
   >重置</a-button
  >
  <a-button type="primary" @click="end">暂停</a-button>
 </div>
</template>

<script>
export default {
 data() {
  return {
   flag: null,
   hour: 0,
   minute: 0,
   second: 0,
   time: "00:00:00",
  };
 },
 methods: {
  //开始计时
  start() {
   this.flag = setInterval(() => {
    this.second = this.second + 1;
    if (this.second >= 60) {
     this.second = 0;
     this.minute = this.minute + 1;
    }

    if (this.minute >= 60) {
     this.minute = 0;
     this.hour = this.hour + 1;
    }
    this.time =
     this.complZero(this.hour) +
     ":" +
     this.complZero(this.minute) +
     ":" +
     this.complZero(this.second);
   }, 1000);
  },
  //重新计时
  reset() {
   window.clearInterval(this.flag);
   this.hour = 0;
   this.minute = 0;
   this.second = 0;
   this.time = "00:00:00";
  },
  //暂停计时
  end() {
   this.flag = clearInterval(this.flag);
  },
  //补零
  complZero(n) {
   return n < 10 ? "0" + n : "" + n;
  },
 },
};
</script>
<style>
.timeContainer {
 font-size: 40px;
 margin-bottom: 10px;
}
</style>

到此这篇关于“用vue怎样写倒计时按钮,代码是什么”的文章就介绍到这了,更多相关用vue怎样写倒计时按钮,代码是什么内容,欢迎关注恒创科技技术资讯频道,小编将为大家输出更多高质量的实用文章!
上一篇: JS中实现判断值是否相等的方法是什么 下一篇: 手机怎么远程登录云服务器?