zoukankan      html  css  js  c++  java
  • vue倒计时:天时分秒

    data数据定义

    data () {
      return {
        curStartTime: '2019-07-31 08:00:00',
        day: '0',
        hour: '00',
        min: '00',
        second: '00',
      }
    },

    js代码

    // 倒计时
    countTime () {
      // 获取当前时间
      let date = new Date()
      let now = date.getTime()
      // 设置截止时间
      let endDate = new Date(this.curStartTime) // this.curStartTime需要倒计时的日期
      let end = endDate.getTime()
      // 时间差
      let leftTime = end - now
      // 定义变量 d,h,m,s保存倒计时的时间
      if (leftTime >= 0) {
        // 天
        this.day = Math.floor(leftTime / 1000 / 60 / 60 / 24)
        // 时
        let h = Math.floor(leftTime / 1000 / 60 / 60 % 24)
        this.hour = h < 10 ? '0' + h : h
        // 分
        let m = Math.floor(leftTime / 1000 / 60 % 60)
        this.min = m < 10 ? '0' + m : m
        // 秒
        let s = Math.floor(leftTime / 1000 % 60)
        this.second = s < 10 ? '0' + s : s
      } else {
        this.day = 0
        this.hour = '00'
        this.min = '00'
        this.second = '00'
      }
      // 等于0的时候不调用
      if (Number(this.hour) === 0 && Number(this.day) === 0 && Number(this.min) === 0 && Number(this.second) === 0) {
        return
      } else {
      // 递归每秒调用countTime方法,显示动态时间效果,
        setTimeout(this.countTime, 1000)
      }
    }, 

    调用

    this.curStartTime = '2019-08-09'
    this.countTime()

    显示

    <p>倒计时:{{day}}天 {{hour}}:{{min}}:{{second}}</p>
  • 相关阅读:
    在浏览器地址栏输入url的后的过程
    webpack的理解
    Vuex总结
    vue 中引用better-scroller实现横向轮播
    vue中类似于jq中的ele.addClass('class').siblings().removeClass('class')效果
    vue中星级判断函数
    ---redux---
    ---react-redux----
    ----flux----
    React组件
  • 原文地址:https://www.cnblogs.com/wgl0126/p/11275140.html
Copyright © 2011-2022 走看看