zoukankan      html  css  js  c++  java
  • vue倒计时(时分秒)组件

    <template>
        <span>{{hour? hourString+':'+minuteString+':'+secondString : minuteString+':'+secondString}}</span>
    </template>
    
    <script>
      export default {
        data () {
          return {
            hour: '',
            minute: '',
            second: '',
            timer: ''
          }
        },
        props: {
          remainTime: {    // 倒计时间总秒数
            default: ''
          }
        },
        mounted () {
          if (this.remainTime > 0) {
            this.hour = Math.floor((this.remainTime / 3600) % 24)
            this.minute = Math.floor((this.remainTime / 60) % 60)
            this.second = Math.floor(this.remainTime % 60)
            this.countDowm()
          }
        },
        methods: {
          countDowm () {
            var self = this
            clearInterval(this.timer)
            this.timer = setInterval(function () {
              if (self.hour === 0) {
                if (self.minute !== 0 && self.second === 0) {
                  self.second = 59
                  self.minute -= 1
                } else if (self.minute === 0 && self.second === 0) {
                  self.second = 0
                  self.$emit('countDowmEnd', true)
                  clearInterval(self.timer)
                } else {
                  self.second -= 1
                }
              } else {
                if (self.minute !== 0 && self.second === 0) {
                  self.second = 59
                  self.minute -= 1
                } else if (self.minute === 0 && self.second === 0) {
                  self.hour -= 1
                  self.minute = 59
                  self.second = 59
                } else {
                  self.second -= 1
                }
              }
            }, 1000)
          },
          formatNum (num) {
            return num < 10 ? '0' + num : '' + num
          }
        },
        computed: {
          hourString () {
            return this.formatNum(this.hour)
          },
          minuteString () {
            return this.formatNum(this.minute)
          },
          secondString () {
            return this.formatNum(this.second)
          }
        }
      }
    </script>
  • 相关阅读:
    环境搭建:Vue环境搭建和项目初始化(windows)
    文件扩展关联命令
    关闭任务栏上右键的打开历史记录
    CDN基本原理和功能浅析
    制作支持UEFI启动的原装系统安装盘
    文件被占用如何查看
    BIOS和CMOS的区别
    PKI公钥基础设施简介
    网络安全通信https工作原理
    常见加密算法简介
  • 原文地址:https://www.cnblogs.com/wy90s/p/10179507.html
Copyright © 2011-2022 走看看