美文网首页
js简单实现倒计时功能(1)

js简单实现倒计时功能(1)

作者: Sven0706 | 来源:发表于2018-06-13 19:53 被阅读0次
// 倒计时(传入截止时间的方式)
  leftTimer(enddate) {
    let leftTime = (new Date(enddate)) - new Date(); //计算剩余的毫秒数
    let days = parseInt(leftTime / 1000 / 60 / 60 / 24, 10); //计算剩余的天数
    let hours = parseInt(leftTime / 1000 / 60 / 60 % 24, 10); //计算剩余的小时
    let minutes = parseInt(leftTime / 1000 / 60 % 60, 10); //计算剩余的分钟
    let seconds = parseInt(leftTime / 1000 % 60, 10); //计算剩余的秒数
    days = this.checkTime(days);
    hours = this.checkTime(hours);
    minutes = this.checkTime(minutes);
    seconds = this.checkTime(seconds);
    if (days <= '00' && hours <= '00' && minutes <= '00' && seconds <= '00') {
      return null;
    }
    if (days >= '00' || hours >= '00' || minutes >= '00' || seconds >= '00') {
      return minutes + ":" + seconds
    }
  }

调用

goDate(v){ //传入时间戳
      let that = this;
      let my_v = this.$utils.formatDateTime(v) //这是把时间戳转换为日期时间格式
      let date1 = new Date();
      let date2 = new Date(my_v);
      if (date2 < date1) {//设置的时间小于现在时间退出
        this.times = '00:00';
        return;
      }else{
        this.ordertimer = setInterval(function () {
          if (that.$utils.leftTimer(date2) == null) {
            clearInterval(that.ordertimer);
            that.times = '00:00';
            that.ordertimer = null;
            return;
          }
          that.times = that.$utils.leftTimer(date2)
        }, 1000);
      }
    }

相关文章

网友评论

      本文标题:js简单实现倒计时功能(1)

      本文链接:https://www.haomeiwen.com/subject/nfjseftx.html