[前端] vue实现点击按钮倒计时

3299 0
Honkers 2022-10-21 15:54:29 | 显示全部楼层 |阅读模式
本文实例为大家分享了vue实现点击按钮倒计时的具体代码,供大家参考,具体内容如下
实现效果:
1.点击开始按钮启动计时


2.点击重置按钮计时恢复到00:00:00
3.点击暂停按钮暂停计时
Vue代码:
  1. <template>
  2. <div>
  3.   <div class="timeContainer">{{ time }}</div>
  4.   <a-button style="margin-right: 20px" type="primary" @click="start"
  5.    >开始</a-button
  6.   >
  7.   <a-button style="margin-right: 20px" type="primary" @click="reset"
  8.    >重置</a-button
  9.   >
  10.   <a-button type="primary" @click="end">暂停</a-button>
  11. </div>
  12. </template>
  13. <script>
  14. export default {
  15. data() {
  16.   return {
  17.    flag: null,
  18.    hour: 0,
  19.    minute: 0,
  20.    second: 0,
  21.    time: "00:00:00",
  22.   };
  23. },
  24. methods: {
  25.   //开始计时
  26.   start() {
  27.    this.flag = setInterval(() => {
  28.     this.second = this.second + 1;
  29.     if (this.second >= 60) {
  30.      this.second = 0;
  31.      this.minute = this.minute + 1;
  32.     }
  33.     if (this.minute >= 60) {
  34.      this.minute = 0;
  35.      this.hour = this.hour + 1;
  36.     }
  37.     this.time =
  38.      this.complZero(this.hour) +
  39.      ":" +
  40.      this.complZero(this.minute) +
  41.      ":" +
  42.      this.complZero(this.second);
  43.    }, 1000);
  44.   },
  45.   //重新计时
  46.   reset() {
  47.    window.clearInterval(this.flag);
  48.    this.hour = 0;
  49.    this.minute = 0;
  50.    this.second = 0;
  51.    this.time = "00:00:00";
  52.   },
  53.   //暂停计时
  54.   end() {
  55.    this.flag = clearInterval(this.flag);
  56.   },
  57.   //补零
  58.   complZero(n) {
  59.    return n < 10 ? "0" + n : "" + n;
  60.   },
  61. },
  62. };
  63. </script>
  64. <style>
  65. .timeContainer {
  66. font-size: 40px;
  67. margin-bottom: 10px;
  68. }
  69. </style>
复制代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持中国红客联盟。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

荣誉红客

关注
  • 4009
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行