countdown.js 3.4 KB
// components/countdown/countdown.js
Component({

  /**
   * 页面的初始数据
   */
  data: {
    day: "00",
    day_one: "0",
    day_two: "0",

    hour: "00",
    hour_one: "0",
    hour_two: "0",

    minute: "00",
    minute_one: "0",
    minute_two: "0",

    second: "00",
    second_one: "0",
    second_two: "0",
  },
  properties: {
    justifyLeft: {
      type: String,
      value: ""
    },
    //距离开始提示文字
    tipText: {
      type: String,
      value: "倒计时"
    },
    dayText: {
      type: String,
      value: "天"
    },
    hourText: {
      type: String,
      value: "时"
    },
    minuteText: {
      type: String,
      value: "分"
    },
    secondText: {
      type: String,
      value: "秒"
    },
    datatime: {
      type: Number,
      value: 0
    },
    isDay: {
      type: Boolean,
      value: true
    }
  },
  created: function () {
    this.show_time();
  },
  methods: {
    show_time: function () {
      let that = this;

      function runTime() {
        //时间函数
        let intDiff = that.data.datatime - Date.parse(new Date()) / 1000; //获取数据中的时间戳的时间差;
        // console.log(that.data.datatime + '结束时间', intDiff + '时间差', Date.parse(new Date()) + '当前时间')
        let day = 0,
          hour = 0,
          minute = 0,
          second = 0;
        if (intDiff > 0) {
          // console.log('时间差大于0')
          //转换时间
          if (that.data.isDay === true) {
            // console.log('时间转换开始')
            day = Math.floor(intDiff / (60 * 60 * 24));

          } else {
            day = 0;
          }
          // console.log(day + '格式化后的天数')
          hour = Math.floor(intDiff / (60 * 60)) - day * 24;
          // console.log(hour + '格式化后的小时')
          minute = Math.floor(intDiff / 60) - day * 24 * 60 - hour * 60;
          // console.log(minute + '格式化后的分钟')
          second =
            Math.floor(intDiff) -
            day * 24 * 60 * 60 -
            hour * 60 * 60 -
            minute * 60;
          // console.log(second + '格式化后的秒')
          if (day <= 9) day = "0" + day;
          if (hour <= 9) hour = "0" + hour;
          if (minute <= 9) minute = "0" + minute;
          if (second <= 9) second = "0" + second;
          // console.log('格式化完成' + day + '天' + hour + '小时' + minute + '分' + second + '秒')
          that.setData({
            day: day,
            day_one: day.toString().substr(0, 1),
            day_two: day.toString().substr(1, 2),
            hour: hour,
            hour_one: hour.toString().substr(0, 1),
            hour_two: hour.toString().substr(1, 2),
            minute: minute,
            minute_one: minute.toString().substr(0, 1),
            minute_two: minute.toString().substr(1, 2),
            second: second,
            second_one: second.toString().substr(0, 1),
            second_two: second.toString().substr(1, 2)
          })


        } else {
          that.setData({
            day: '00',
            day_one: '0',
            day_two: '0',
            hour: '00',
            hour_one: '0',
            hour_two: '0',
            minute: '00',
            minute_one: '0',
            minute_two: '0',
            second: '00',
            second_one: '0',
            second_two: '0'
          })

        }
      }
      runTime();
      setInterval(runTime, 1000);
    }
  }
})