分秒倒计时

data() {
    return {
      timeLeft: ""
    };
  },
//在mounted中调用
 mounted() {
    setInterval(this.countTime, 1000);
  },
//方法如下
methods: {
    countTime() {
      //获取当前时间
      let date = new Date();
      let now = date.getTime();
      //设置截止时间
      //let end = new Date(this.detail.CreatedDate).getTime() + 15 * 60 * 1000;//this.detail.CreatedDate可以是后端返回的创建时间
      let end = new Date("2018-05-11 15:02").getTime() + 15 * 60 * 1000;
      //console.log(end, "结束时间");
      //时间差
      let leftT = end - now;
      //console.log(leftT, "时间差");
      //定义变量 d,h,m,s保存倒计时的时间
      let d, h, m, s;
      if (leftT >= 0) {
        //d = Math.floor(leftT / 1000 / 60 / 60 / 24);
        //h = Math.floor((leftT / 1000 / 60 / 60) % 24);
        m = Math.floor((leftT / 1000 / 60) % 60);
        s = Math.floor((leftT / 1000) % 60);
      } else {
        m = 0;
        s = 0;
      }
      clearInterval(this.countTime);
      //将倒计时赋值到data中
      this.timeLeft = `${m}分${s<10 ? `0${s}` : `${s}` }秒`;
    }
  }
var timerHourDown;
function getTime(timeStamp) {
    //时分倒计时
    if (timeStamp > 0) {
        var h = 0;
        var m = 0;
        var s = 0;
        function countDown() {
            $('.icon-dater').show()
            if (timeStamp <= 0) {
                clearInterval(timerHourDown)
                $('.icon-dater').hide()
            }
            timeStamp -= 1000
            h = Math.floor((timeStamp / 1000 / 60 / 60) % 24);
            m = Math.floor((timeStamp / 1000 / 60) % 60);
            s = Math.floor((timeStamp / 1000) % 60);
            //console.log(timeStamp, s);
            $('.icon-dater').empty().html(h + "小时" + m + "分后自动确认")
        }
        timerHourDown = setInterval(countDown, 1000);
    } else {
        setTimeout(function () {
            $('.icon-dater').hide()
        }, 0)
        clearInterval(timerHourDown)
    }
}

    var d = parseInt(timeStamp / (24 * 60 * 60 * 1000));//天数
    var h = parseInt(timeStamp / (60 * 60 * 1000) % 24);//当前天内小时
    var hh = parseInt(timeStamp / (60 * 60 * 1000));//单纯剩余小时
    var m = parseInt(timeStamp / (60 * 1000) % 60);//当前小时内剩余分钟
    var mm = parseInt(timeStamp / 60 / 1000);//单纯剩余分钟
    var s = parseInt(timeStamp / 1000 % 60);//当前分钟内剩余秒数
    var ss = parseInt(timeStamp / 1000);//单纯剩余秒数

你可能感兴趣的:(分秒倒计时)